Note that there are some explanatory texts on larger screens.

plurals
  1. POprocess sync using sigusr1 and sigusr2
    primarykey
    data
    text
    <p>i am writing a producer process to write a block of data to a file, and a consumer process to read the same block, but the below code gets stuck in an infinite loop, now from my side i think i am having a problem in the consumer process implementation!!</p> <pre><code>void wakeup() { ; }; unsigned int sleep ( unsigned int timer ) { if (sigset(SIGALRM, wakeup)==-1) { perror("sigset"); return 1; } (void)alarm( timer ); (void)pause(); return 0; } int main(void) { int fd, n, i, SomeInt, DataRead; pid_t pid, ppid; char buf[4]; char *buf2; int x=0; if((fd=open("/tmp/data_file", O_APPEND|O_CREAT, 0640)) &lt;0) exit(1); sigset(SIGTERM,SIG_IGN);/* signal */ ; sigset(SIGINT,SIG_IGN); /* signal */ pid=fork(); switch (pid) { case -1: { perror("FORK"); exit(1); } break; case 0: /* child process - Producer */ sigset(SIGUSR1,wakeup); sighold(SIGUSR1); /* block / hold signals SIGUSR1 until sigpause*/ FILE *file = fopen ("binary.txt", "r"); while (!feof(file)) { n = (int)(getpid()%256); srand((unsigned)n); sleep(rand() %5); for( x=0; x&lt;=4;x++) fscanf (file, "", buf[x]); write(fd, buf,sizeof(buf)); ppid=getppid(); kill(ppid, SIGUSR2); sigpause(SIGUSR1); } fclose(file); fflush(stdin); break; default: /* -parent code - Consumer */ sigset(SIGUSR2,wakeup); sighold(SIGUSR2); /* block / hold signals SIGUSR2 until sigpause*/ for (i=0; i&lt;=100; i++) { /* sleep a random amount of time */ n = (int)(getpid()%256); srand((unsigned)n); sleep(rand() %5); sigpause(SIGUSR2); /* pause(); */ /* reads a character from file */ read(fd, buf,sizeof(buf)); fprintf(stderr,"Consumer PID=%d value=%d\n",getpid(),atoi(buf)); kill(pid, SIGUSR1) ; } break; } exit(0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload