different results with printf and fprintf - c++

I need function that prints "word=n" (where n in [0..10]) to stream using linux function ssize_t write(int fd, const void *buf, size_t count);. Trying to use fprintf, but it's give strange results : program prints in ~1% of calls "woword=n", and length for example "woword=7" are 7. Printf print all right. I'm doing something wrong or this is the bag ?
if ((id_result = open( out , O_WRONLY)) <= 0) {
fprintf(stderr, "%s : %s\n", currentDateTime().c_str(), "could not open output\0");
ret = P_STREAMS_LOAD_ERROR;
}
void printProbability( int probability ){
char buf[50];
memset( buf, '\0', 50 );
int length = sprintf( buf, "word=%i\n\0", probability );
fprintf(stderr, "debug : word=%i len = %i\n\0", probability, length );
int result = write( id_result, buf, length );
if( result == -1){
fprintf(stderr, "%s : %s\n", currentDateTime().c_str(), "error \n");
}
}
EDITED:
how I understand, we have 2 theorys :
1) mixing printf and write
2) using '\0' and '\n' in fprintf
int length = sprintf( buf, "word=%i", probability );
int result = write( id_result, buf, length );
write( id_result, "\n", 1 );
with this code I still have same errors
aa help me :))

If you are interspersing calls to printf (or write) and fprintf(stderr, ...) the output won't necessarily come out in order. There is buffering going on, and the actual output probably won't switch at the end-of-line character.

Related

FIFO Communication between two different process c++

Hi please some one help me
I've two process say some X and Y.
X and Y both have the following information
typedef enum {
HEALTHY=1,
FAULTY=2,
CHANGE=3,
ERROR=4
} MsgTypeT;
typedef struct {
char role[16];
char state[16];
char info[256];
} InfoT;
typedef struct {
MsgTypeT type;
int size;
InfoT *data;
} MsgT;
Here the condition is that if process Y sends an information process X will read it
So i used fifo between x and y
Y has a function write buffer which writes to fifo and code is as following
int write_buffer(HA_DEVMON_MsgT const* send)
{
char* dest = buffer;
memcpy( dest, &send->type, sizeof( MsgTypeT ));
dest += sizeof(MsgTypeT);
memcpy( dest, &send->size, sizeof( int ));
dest += sizeof(int);
memcpy( dest, send->data, sizeof( InfoT ));
dest += sizeof(InfoT);
int byteCount = write( this->fifo_fd, buffer, dest - buffer );
if ( byteCount != dest - buffer ) {
cout<<"Error in writing ";
}
return byteCount == dest - buffer ? 0 : -1;
}
I think it's writing perfectly because cout statements are working fine also when tried to output nbytes it gave 512bytes have been written
Now when X tries to read it's giving null values for role and state also size its giving 6441568
Its only giving MsgTypeT correct other values are null :(
The code is as follows--- I'm doing something wrong please correct it
int readMsg(MsgT *msg)
{
int rCode=0, nbytes=0;
char buffer[512]={0};
nbytes = read(this->get_handle(), buffer, sizeof(buffer));
if (nbytes < 0) {
cout<<"error in read";
rCode=-1;
}
if (rCode == 0) {
char *p_src = (char *)buffer;
mempcpy(&msg->type, p_src, sizeof(MsgTypeT));
p_src+=sizeof(MsgTypeT);
mempcpy(&msg->size, p_src, sizeof(int));
p_src+=sizeof(int);
msg->data = new InfoT(); //allocating memory (needed or not???)
mempcpy(msg->data, p_src, sizeof(InfoT));
p_src+=sizeof(InfoT);
}
return rCode;
}
In readMsg, your last mempcpy writes to msg, not to the
InfotT you just allocated.
Also, but I suppose you know this: this is only guaranteed to
work if both processes were compiled with the same compiler,
using the same options. (In practice, it's likely to work if
the underlying system defines its API in terms of C, which is
the case for Windows and Unix.)
EDIT:
Further: you have the same problem when writing. You write
sizeof(InfoT) (288) bytes, but you write the pointer (and then
a lot of garbage), not the data it's pointing to.
And you increment the pointer into the MsgT object. This is
likely not to work, if there is any padding. What you really
have to do is:
int
write_buffer( MsgT const* data )
{
char buffer[512] = {}; // Or better yet, std::vector<char>
char* dest = buffer;
memcpy( dest, &data->type, sizeof( MsgTypeT ) );
dest += sizeof( MsgTypeT );
memcpy( dest, &data->size, sizeof( int ) );
dest += sizeof( int );
memcpy( dest, &data->data, sizeof( InfoT ) );
dest += sizeof( InfoT );
int byteCount = write( fifo_fd, buffer, dest - buffer );
if ( byteCount != dest - buffer ) {
std::cerr << "Error in write" << std::endl;
}
return byteCount == dest - buffer ? 0 : -1;
}
and the opposite when reading.
And once again, this will only really work for two processes on
the same machine, compiled with the same compiler using the same
options. A better solution would probably be to define
a protocol, with a defined representation of integers, strings,
etc., format your output to that representation, and parse it
for your input. That way, it will still work even if one of the
processes is 64 bits, and the other 32.

C++ fwrite() in hex

Writing code with Winsock.
I currently have this:
fwrite(buff, 1, len, stdout);
How to do it like:
for ( int i = 0; i < len; i++ ) {
printf( "%02x ", unsigned char (buff[i]) );
}
printf( "\n" );
Or should I just remove the fwrite and use the print instead?
I wanted to write it to stdout, cuz I have my option to either write to stdout of write to file.
fprintf (see the docs) is like printf but to an arbitrary file:
fprintf(stdout, "%02x ", unsigned char (buff[i]));

Calculating %CPU Usage Using Proc Files

I would like to dedicate a thread in my program to gathering metrics on its performance. Memory usage, CPU etc. I've been trying to do this using the /proc/stat and /proc/pid/stat files. I'm currently stuck at trying to measure the %CPU usage. The values reported by my program are totally out of line with what 'top' is reporting. I'm tried this on a few different linux distros and am seeing the same results on each.
Here is the code I am using to calculate the percentage. Can anyone spot any issues here?
https://github.com/mmcilroy/cpu_usage
#include <stdlib.h>
#include <sys/types.h>
#include <sys/times.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
struct pstat {
long unsigned int utime_ticks;
long int cutime_ticks;
long unsigned int stime_ticks;
long int cstime_ticks;
long unsigned int vsize; // virtual memory size in bytes
long unsigned int rss; //Resident Set Size in bytes
long unsigned int cpu_total_time;
};
int get_usage(const pid_t pid, struct pstat* result) {
//convert pid to string
char pid_s[20];
snprintf(pid_s, sizeof(pid_s), "%d", pid);
char stat_filepath[30] = "/proc/"; strncat(stat_filepath, pid_s,
sizeof(stat_filepath) - strlen(stat_filepath) -1);
strncat(stat_filepath, "/stat", sizeof(stat_filepath) -
strlen(stat_filepath) -1);
FILE *fpstat = fopen(stat_filepath, "r");
if (fpstat == NULL) {
perror("FOPEN ERROR ");
return -1;
}
FILE *fstat = fopen("/proc/stat", "r");
if (fstat == NULL) {
perror("FOPEN ERROR ");
fclose(fstat);
return -1;
}
//read values from /proc/pid/stat
bzero(result, sizeof(struct pstat));
long int rss;
if (fscanf(fpstat, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu"
"%lu %ld %ld %*d %*d %*d %*d %*u %lu %ld",
&result->utime_ticks, &result->stime_ticks,
&result->cutime_ticks, &result->cstime_ticks, &result->vsize,
&rss) == EOF) {
fclose(fpstat);
return -1;
}
fclose(fpstat);
result->rss = rss * getpagesize();
//read+calc cpu total time from /proc/stat
long unsigned int cpu_time[10];
bzero(cpu_time, sizeof(cpu_time));
if (fscanf(fstat, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
&cpu_time[0], &cpu_time[1], &cpu_time[2], &cpu_time[3],
&cpu_time[4], &cpu_time[5], &cpu_time[6], &cpu_time[7],
&cpu_time[8], &cpu_time[9]) == EOF) {
fclose(fstat);
return -1;
}
fclose(fstat);
for(int i=0; i < 4;i++)
result->cpu_total_time += cpu_time[i];
printf( "usage: cpu %lu, utime %lu, stime %lu\n", result->cpu_total_time, result->utime_ticks, result->stime_ticks );
return 0;
}
void calc_cpu_usage_pct(const struct pstat* cur_usage,
const struct pstat* last_usage,
double* usage)
{
printf( "delta: cpu %lu, utime %lu, stime %lu\n",
cur_usage->cpu_total_time - last_usage->cpu_total_time,
cur_usage->utime_ticks - last_usage->utime_ticks,
cur_usage->stime_ticks - last_usage->stime_ticks );
const long unsigned int cpu_diff = cur_usage->cpu_total_time - last_usage->cpu_total_time;
const long unsigned int pid_diff =
( cur_usage->utime_ticks + cur_usage->utime_ticks + cur_usage->stime_ticks - cur_usage->stime_ticks ) -
( last_usage->utime_ticks + last_usage->utime_ticks + last_usage->stime_ticks - last_usage->stime_ticks );
*usage = 100.0 * ( (double)pid_diff / (double)cpu_diff );
}
int main( int argc, char* argv[] )
{
pstat prev, curr;
double pct;
struct tms t;
times( &t );
if( argc <= 1 ) {
printf( "please supply a pid\n" ); return 1;
}
while( 1 )
{
if( get_usage(atoi(argv[1]), &prev) == -1 ) {
printf( "error\n" );
}
sleep( 5 );
if( get_usage(atoi(argv[1]), &curr) == -1 ) {
printf( "error\n" );
}
calc_cpu_usage_pct(&curr, &prev, &pct);
printf("%%cpu: %.02f\n", pct);
}
}
If you want to try it out for yourself, the program expect 1 arguments - the pid of a process to monitor
I know this is a bit old but I can explain why your new equation works: (1/INTERVAL) * (pid diff)
It's just a simplification of the basic percentage equation 100 * (pid diff) / (cpu diff), which looks like what you were trying to do in your first example.
The cpu time in /proc/stat (and the utime and stime in /proc/pid/stat) is reported in USER_HZ (or jiffies). This value is usually 1/100 of a second. This means that there will be 100 "tics" in each second for the CPU, which means your "CPU diff" will be INTERVAL*100.
Substitute that in and you get:
100 * (pid diff) / (INTERVAL * 100)
Cancel out the 100's and you are left with:
(pid diff) / INTERVAL
Which is the same as what you are now using. This also means that if you did indeed correct the problems you have in the code at the top, then that should work as well. The pid diff should be (curr utime + curr stime) - (prev utime + prev stime). If it doesn't work, then perhaps the way you are adding up the CPU time is wrong? It'd be easy to test because you know what value it should be (INTERVAL*100).
Since you now have a working equation, you may not care to figure out the problem with the original code but keep in mind that if you ever try to use it on a system where USER_HZ is not 1/100, the equation will be invalid.
I examined the source for top (from procps). Seems it is essentially performing the following calculation...
(1/interval) * (utime+stime)
Where interval it the number of seconds between samples. utime / stime are read directly from /proc/pid/stat
I must admit I don't understand why this works (it shouldn't according to "man proc"), but I've tested this with numerous different scenarios and the output from my program always matches that of "top".
Would be interested to hear some feedback on why this works :)
Here's my latest source
#include <stdlib.h>
#include <sys/types.h>
#include <sys/times.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define INTERVAL 3
struct pstat {
long unsigned int utime_ticks;
long int cutime_ticks;
long unsigned int stime_ticks;
long int cstime_ticks;
long unsigned int vsize; // virtual memory size in bytes
long unsigned int rss; //Resident Set Size in bytes
};
int get_usage(const pid_t pid, struct pstat* result) {
//convert pid to string
char pid_s[20];
snprintf(pid_s, sizeof(pid_s), "%d", pid);
char stat_filepath[30] = "/proc/"; strncat(stat_filepath, pid_s,
sizeof(stat_filepath) - strlen(stat_filepath) -1);
strncat(stat_filepath, "/stat", sizeof(stat_filepath) -
strlen(stat_filepath) -1);
FILE *fpstat = fopen(stat_filepath, "r");
if (fpstat == NULL) {
perror("FOPEN ERROR ");
return -1;
}
//read values from /proc/pid/stat
bzero(result, sizeof(struct pstat));
long int rss;
if (fscanf(fpstat, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu"
"%lu %ld %ld %*d %*d %*d %*d %*u %lu %ld",
&result->utime_ticks, &result->stime_ticks,
&result->cutime_ticks, &result->cstime_ticks, &result->vsize,
&rss) == EOF) {
fclose(fpstat);
return -1;
}
fclose(fpstat);
result->rss = rss * getpagesize();
return 0;
}
void calc_cpu_usage_pct(const struct pstat* cur_usage,
const struct pstat* last_usage,
double* usage)
{
const long unsigned int pid_diff =
( cur_usage->utime_ticks + cur_usage->stime_ticks ) -
( last_usage->utime_ticks + last_usage->stime_ticks );
printf( "delta %lu\n", pid_diff );
*usage = 1/(float)INTERVAL * pid_diff;
}
int main( int argc, char* argv[] )
{
pstat prev, curr;
double pct;
struct tms t;
times( &t );
if( argc <= 1 ) {
printf( "please supply a pid\n" ); return 1;
}
while( 1 )
{
if( get_usage(atoi(argv[1]), &prev) == -1 ) {
printf( "error\n" );
}
sleep( INTERVAL );
if( get_usage(atoi(argv[1]), &curr) == -1 ) {
printf( "error\n" );
}
calc_cpu_usage_pct(&curr, &prev, &pct);
printf("%%cpu: %.02f\n", pct);
}
}
This command in linux may be useful for linux.
# apt-get install sysstat
# up2date sysstat
# mpstat
Now you find how to find how to get command line output as string and parse. You can also use different parameters of mpstat.
Also try $ top.
Get help from this link.
The main loop is somewhat off: instead of getting "prev", then sleeping, then getting "next" and calculating the difference, you should get "prev" once outside the loop, and inside the loop get "curr", calculate, the copy "curr" into "prev" and then loop again. This fixes the part where 50% of the used time is not counted.
try seeing the top command source code , source code will be available in busybox
EDIT:
replace mpstat with top as mpstat shows overall usage

Unicode File Writing and Reading in C++?

Can anyone Provide a Simple Example to Read and Write in the Unicode File a Unicode Character ?
try http://utfcpp.sourceforge.net/. the link has an introductory example to read a utf8 file, line by line.
On linux I use the iconv (link) library which is very standard. An overly simple program is:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#define BUF_SZ 1024
int main( int argc, char* argv[] )
{
char bin[BUF_SZ];
char bout[BUF_SZ];
char* inp;
char* outp;
ssize_t bytes_in;
size_t bytes_out;
size_t conv_res;
if( argc != 3 )
{
fprintf( stderr, "usage: convert from to\n" );
return 1;
}
iconv_t conv = iconv_open( argv[2], argv[1] );
if( conv == (iconv_t)(-1) )
{
fprintf( stderr, "Cannot conver from %s to %s\n", argv[1], argv[2] );
return 1;
}
bytes_in = read( 0, bin, BUF_SZ );
{
bytes_out = BUF_SZ;
inp = bin;
outp = bout;
conv_res = iconv( conv, &inp, &bytes_in, &outp, &bytes_out );
if( conv_res >= 0 )
{
write( 1, bout, (size_t)(BUF_SZ) - bytes_out );
}
}
iconv_close( conv );
return 0;
}
This is overly simple to demonstrate the conversion. In the real world you would normally have two nested loops:
One reading input, so handle when its more than BUF_SZ
One converting input to output. Remember if you're converting from ascii to UTF-32LE you will end up with each iunput byte being 4 bytes of output. So the inner loop would handle this by examining conv_res and then checking errno.
In case you're using Windows.
Use fgetws http://msdn.microsoft.com/en-us/library/c37dh6kf(VS.71).aspx to read
and fputws http://msdn.microsoft.com/en-us/library/t33ya8ky(VS.71).aspx to write.
The example code are in the provided links.

iconv encoding conversion problem

I am having trouble converting strings from utf8 to gb2312. My convert function is below
void convert(const char *from_charset,const char *to_charset, char *inptr, char *outptr)
{
size_t inleft = strlen(inptr);
size_t outleft = inleft;
iconv_t cd; /* conversion descriptor */
if ((cd = iconv_open(to_charset, from_charset)) == (iconv_t)(-1))
{
fprintf(stderr, "Cannot open converter from %s to %s\n", from_charset, to_charset);
exit(8);
}
/* return code of iconv() */
int rc = iconv(cd, &inptr, &inleft, &outptr, &outleft);
if (rc == -1)
{
fprintf(stderr, "Error in converting characters\n");
if(errno == E2BIG)
printf("errno == E2BIG\n");
if(errno == EILSEQ)
printf("errno == EILSEQ\n");
if(errno == EINVAL)
printf("errno == EINVAL\n");
iconv_close(cd);
exit(8);
}
iconv_close(cd);
}
This is an example of how I used it:
int len = 1000;
char *result = new char[len];
convert("UTF-8", "GB2312", some_string, result);
edit: I most of the time get a E2BIG error.
outleft should be the size of the output buffer (e.g. 1000 bytes), not the size of the incoming string.
When converting, the string length usually changes in the process and you cannot know how long it is going to be until afterwards. E2BIG means that the output buffer wasn't large enough, in which case you need to give it more output buffer space (notice that it has already converted some of the data and adjusted the four variables passed to it accordingly).
As others have noted, E2BIG means that the output buffer wasn't large enough for the conversion and you were using the wrong value for outleft.
But I've also noticed some other possible problems with your function. Namely, with the way your function works, your caller has no way of knowing how many bytes are in the output string. Your convert() function neither nul-terminates the output buffer nor does it have a means of telling its caller the number of bytes it wrote to outptr.
If you want to deal with nul-terminates strings (and it appears that's what you want to do since your input string is nul-terminated), you might find the following approach to be much better:
char *
convert (const char *from_charset, const char *to_charset, const char *input)
{
size_t inleft, outleft, converted = 0;
char *output, *outbuf, *tmp;
const char *inbuf;
size_t outlen;
iconv_t cd;
if ((cd = iconv_open (to_charset, from_charset)) == (iconv_t) -1)
return NULL;
inleft = strlen (input);
inbuf = input;
/* we'll start off allocating an output buffer which is the same size
* as our input buffer. */
outlen = inleft;
/* we allocate 4 bytes more than what we need for nul-termination... */
if (!(output = malloc (outlen + 4))) {
iconv_close (cd);
return NULL;
}
do {
errno = 0;
outbuf = output + converted;
outleft = outlen - converted;
converted = iconv (cd, (char **) &inbuf, &inleft, &outbuf, &outleft);
if (converted != (size_t) -1 || errno == EINVAL) {
/*
* EINVAL An incomplete multibyte sequence has been encounĀ­-
* tered in the input.
*
* We'll just truncate it and ignore it.
*/
break;
}
if (errno != E2BIG) {
/*
* EILSEQ An invalid multibyte sequence has been encountered
* in the input.
*
* Bad input, we can't really recover from this.
*/
iconv_close (cd);
free (output);
return NULL;
}
/*
* E2BIG There is not sufficient room at *outbuf.
*
* We just need to grow our outbuffer and try again.
*/
converted = outbuf - out;
outlen += inleft * 2 + 8;
if (!(tmp = realloc (output, outlen + 4))) {
iconv_close (cd);
free (output);
return NULL;
}
output = tmp;
outbuf = output + converted;
} while (1);
/* flush the iconv conversion */
iconv (cd, NULL, NULL, &outbuf, &outleft);
iconv_close (cd);
/* Note: not all charsets can be nul-terminated with a single
* nul byte. UCS2, for example, needs 2 nul bytes and UCS4
* needs 4. I hope that 4 nul bytes is enough to terminate all
* multibyte charsets? */
/* nul-terminate the string */
memset (outbuf, 0, 4);
return output;
}