SHARE
TWEET

Untitled

a guest Jan 14th, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <wait.h>
  12. #define PCHILD 10
  13.  
  14. int fd[2];
  15. pid_t pid;
  16. void WaitId();
  17. int con=0;
  18. void SignalHandler(int sig,siginfo_t* info,void* text)
  19. {
  20.     srand(255);
  21.     char c=90;
  22.     write(fd[1],&c,1);
  23.     ++con;
  24.     if(con==3)
  25.         close(fd[1]);
  26. }
  27. int main(int argc,char** argv)
  28. {
  29.  pipe(fd);
  30.  int i=0;
  31.  for(i=0;i<PCHILD;++i)
  32.  {
  33.     pid=fork();
  34.     if(!pid)
  35.     {
  36.         char read[4];
  37.         char write[4];
  38.         sprintf(read,"%d",fd[0]);
  39.         sprintf(write,"%d",fd[1]);
  40.         execlp("./child","child",read,write,(char*)NULL);
  41.         perror("Execlp failed\n");
  42.         exit(-1);
  43.     }
  44.     else if(pid==-1)
  45.     {
  46.         perror("Fork failed\n");
  47.         exit(-1);
  48.     }
  49.  }
  50.  close(fd[0]);
  51.  
  52.  struct sigaction act;
  53.  sigemptyset(&act.sa_mask);
  54.  act.sa_sigaction=&SignalHandler;
  55.  act.sa_flags=SA_SIGINFO;
  56.  if(sigaction(SIGRTMIN+4,&act,NULL)==-1)
  57.  {
  58.  
  59.      perror("Sigaction failed\n");
  60.      killpg(getpgrp(),SIGTERM);
  61.  }
  62.  timer_t timerid;
  63.  struct sigevent sevp;
  64.  sevp.sigev_notify=SIGEV_SIGNAL;
  65.  sevp.sigev_signo=SIGRTMIN+4;
  66.  if(timer_create(CLOCK_REALTIME,&sevp,&timerid)==-1)
  67.  {
  68.      perror("timer create failed\n");
  69.      exit(-1);
  70.  }
  71.  struct itimerspec it;
  72.  struct itimerspec it_old;
  73.  it.it_value.tv_sec=0;
  74.  it.it_value.tv_nsec=500000000;
  75.  it.it_interval.tv_sec=0;
  76.  it.it_interval.tv_nsec=500000000;
  77.  if(timer_settime(timerid,0,&it,&it_old)==-1)
  78.  {
  79.     printf("timer_settime failed\n");
  80.     errno;
  81.     killpg(getpgrp(),SIGTERM);
  82.  }
  83.    int ret;
  84.    siginfo_t info;
  85.    while(1)
  86.    {
  87.          ret=waitid(P_ALL,0,&info,WEXITED|WSTOPPED|WCONTINUED|WNOHANG);
  88.          if(ret==-1)
  89.                 break;
  90.          if(!ret && info.si_pid)
  91.                printf("PID:%d Status:%d\n",info.si_pid,info.si_status);
  92.    }
  93.  close(fd[1]);
  94.  return 0;
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. #define _GNU_SOURCE
  112. #include <stdio.h>
  113. #include <sys/stat.h>
  114. #include <fcntl.h>
  115. #include <unistd.h>
  116. #include <sys/types.h>
  117. #include <sys/wait.h>
  118. #include <stdlib.h>
  119. #include <errno.h>
  120. #include <time.h>
  121. #include <wait.h>
  122. #include <poll.h>
  123.  
  124. int main(int argc,char** argv)
  125. {
  126.  char* end1=NULL;
  127.  char* end2=NULL;
  128.  int rd=strtol(argv[1],&end1,10);
  129.  int wr=strtol(argv[2],&end2,10);
  130.  if(end1==argv[1] || end2==argv[2])
  131.      exit(-1);
  132.  if(fcntl(rd,F_GETFD)==-1 || fcntl(wr,F_GETFD)==-1 || errno==EBADF)
  133.  {
  134.      perror("File descriptor read or write is invalid\n");
  135.      exit(-1);
  136.  }
  137.  time_t start,end;
  138.  struct pollfd fds;
  139.  fds.fd=rd;
  140.  fds.events=POLLIN;
  141.  struct timespec tm;
  142.  tm.tv_sec=0;
  143.  tm.tv_nsec=500000000;
  144.  while(1)
  145.  {
  146.      char c;
  147.      if(ppoll(&fds,1,&tm,NULL)>0)
  148.      { 
  149.         read(rd,&c,1);
  150.         time(&start);
  151.         struct timespec req;
  152.         if(c>48)
  153.         {
  154.             write(1,&c,1);
  155.             (int)c--;
  156.             write(wr,&c,1);
  157.             time(&end);
  158.         }
  159.         req.tv_sec=0;
  160.         req.tv_nsec=1000000;
  161.         nanosleep(&req,NULL);
  162.      }
  163.      else
  164.      {
  165.         close(wr);
  166.         close(rd);
  167.         exit(0);
  168.      }
  169.  }
  170.  
  171.  return 1 ;
  172. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top