c - Write the integer 0 to file, read the file, increment integer to 10, and arrange it through the child and parent process -


this has been done before, trouble code not incrementing, doing else fine. want have output of file reads integer loop going to. instead, gets stuck @ 0. need help. writing in c. here code:

assignment9.c:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include "tellwait.h"  void err_sys(const char* message);  int main(void){     file *filepoint;     int* 0 = 0;     int val;     char* buffer[180];     pid_t pid;     filepoint = fopen("testfile.txt", "w+");     fprintf(filepoint, "%d", zero);      tell_wait();     if((pid = fork()) < 0)     {         err_sys("fork error");     }     else if (pid == 0)  //child     {         int i;         for(i = 0; < 10; i++){             wait_parent();             filepoint = fopen("testfile.txt", "w+");             fread(buffer, sizeof(zero), 1, filepoint);             // increment             val = atoi(buffer[0]);             val++;             // use fprintf write file             //printf("current amount: %d, child process\n", zero);             printf("%d\n", val);             fprintf(filepoint, "%d", val);              tell_parent(pid);          }     }     else {    //parent         int i;                   for(i = 0; < 10; i++){             filepoint = fopen("testfile.txt", "w+");             fread(buffer, sizeof(zero), 1, filepoint);             // increment             val = atoi(buffer[0]);             val++;             // use fprintf write file             //printf("current amount: %d, parent process\n", zero);             printf("%d\n", val);             fprintf(filepoint, "%d", val);             tell_child(pid);             wait_child();         }     }     return exit_success; }  void err_sys(const char* message) {     printf("%s\n", message);     exit(0); } 

tellwait.h:

#ifndef _tell_wait_h #define _tell_wait_h  void    tell_wait(void); /* parent/child {sec race_conditions} */ void    tell_parent(pid_t); void    tell_child(pid_t); void    wait_parent(void); void    wait_child(void);  #endif 

tellwait.c:

#include <stdlib.h> #include <signal.h>  void err_sys (const char* message);  static volatile sig_atomic_t sigflag; /* set nonzero sig handler */ static sigset_t newmask, oldmask, zeromask;  static void sig_usr(int signo)  /* 1 signal handler sigusr1 ,     sigusr2 */ {    sigflag = 1; }  void tell_wait(void) {    if (signal(sigusr1, sig_usr) == sig_err)        err_sys("signal(sigusr1) error");    if (signal(sigusr2, sig_usr) == sig_err)        err_sys("signal(sigusr2) error");    sigemptyset(&zeromask);    sigemptyset(&newmask);    sigaddset(&newmask, sigusr1);    sigaddset(&newmask, sigusr2);     /* block sigusr1 , sigusr2, , save current signal mask */    if (sigprocmask(sig_block, &newmask, &oldmask) < 0)        err_sys("sig_block error");    }  void tell_parent(pid_t pid) {    kill(pid, sigusr2);      /* tell parent we're done */ }  void wait_parent(void) {    while (sigflag == 0)        sigsuspend(&zeromask);   /* , wait parent */    sigflag = 0;     /* reset signal mask original value */    if (sigprocmask(sig_setmask, &oldmask, null) < 0)        err_sys("sig_setmask error"); }  void tell_child(pid_t pid) {    kill(pid, sigusr1);          /* tell child we're done */ }  void wait_child(void) {    while (sigflag == 0)       sigsuspend(&zeromask);    /* , wait child */    sigflag = 0;     /* reset signal mask original value */    if (sigprocmask(sig_setmask, &oldmask, null) < 0)       err_sys("sig_setmask error"); } 

the file output testfile.txt 10, while test console output attempting is:

1 2 3 4 5 6 7 8 9 10 

may please me?


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -