Question

Please read the question carefully and in full and then answer it (using C language and any necessary libraries).

(I am not sure what the expert's mean by when to move to using pipe. I guess if you need to, then you can use a random time or any exact time but make sure to put a comment next to it).

Use fork) to create a child process that generates the data for events, a source elapsedtime, and writes these into a pipe. T

you might use the below code to give you some idea:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>


void main()
{
   int fd1[2];
   int fd2[2];
   int pid1 = 0, pid2 = 0;

/* register pipe 1 */
   if(pipe(fd1) == -1)
   {
       perror("Pipe1");
       return;
   }
/* register pipe 2 */
   if(pipe(fd2) == -1)
   {
       perror("Pipe2");
       return;
   }
/* create child 1 process */
   if((pid1 = fork()) == 0)
   {
       int i=0;
       char data[64] = {0};
       /* send data in pipe 1 from 1 to 100 */
       for(i=1; i<=100; i++)
       {
           memset(data, 0 , sizeof(data)); // clear buffer
           sprintf(data,"child - 1, data - %d\n",i); // copy data in child 1 into data
           write(fd1[1],data,strlen(data)); // send data over pipe
           sleep(0.01);
       }
       memset(data, 0 , sizeof(data)); // clear buffer
       sprintf(data,"end");
       write(fd1[1],data,strlen(data)); // send end to pipe to indicate data is over
       close(fd1[1]);

   }
   else
   {
       /* create child 2 process */
       if((pid2 = fork()) == 0)
       {
           int i=0;
           char data[64] = {0};
           /* send data in pipe 1 from 1 to 100 */
           for(i=1; i<=100; i++)
           {
               memset(data, 0 , sizeof(data)); // clear buffer
               sprintf(data,"child - 2, data - %d\n",i); // copy data and child 2 in data
               write(fd2[1],data,strlen(data)); // send data in pipe
               sleep(0.01);
           }
           memset(data, 0 , sizeof(data)); // clear buffer
           sprintf(data,"end");
           write(fd2[1],data,strlen(data)); // send end to pipe to indicate data is over
           close(fd2[1]);
       }
       else
       {
           char data1[64] = {0};
           char data2[64] = {0};
           int status = 0;
           int flag1 = 0;
           int flag2 = 0;
           close(fd1[1]); // close fd1 writing end
           close(fd2[1]); // close fd1 writing end
           while(1)
           {
              
               memset(data1, 0 ,sizeof(data1)); // clean buffer
               memset(data2, 0 ,sizeof(data2));
               if(flag1 != 1 )
                   read(fd1[0], data1, sizeof(data1)); // read from pipe 1
               if(flag2 != 1)
                   read(fd2[0], data2, sizeof(data2)); // read from pipe 2
              
               if(strstr(data1,"end")) // if got end close pipe
               {
                   printf("end 1\n");
                   if(flag2 == 1) // if child 2 process over
                       break; // break the loop
                   flag1 =1;
                   close(fd1[0]); // close fd1 reading end
               }
               else if(strstr(data2, "end"))
               {
                   printf("end 2\n");
                   if(flag1 == 1) // if child 1 process over
                       break; // break the while loop
                   flag2 = 1;
                   close(fd2[0]); // close fd2 reading end
               }
               if(strstr(data1,"child") && strstr(data2,"child"))
               {
                   if(flag1 != 1)
                       printf("%s\n",data1);
                   if(flag2 != 1)
                       printf("%s\n",data2);
               }

              

           }
           waitpid(-1, &status, 0); // wait for child to complite
           waitpid(-1, &status, 0); // wait for child to complite
          
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Fork-It is used to create new process duplicating the calling process

USING WAIT PID

#include <stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include <sys/wait.h>

void child process (void); /child process/

void parent process(void); /parent process/

void main(void)

{

pid_t pid;

pid =fork();

if(Pid==0)

child process();

else

parent process();

}

void Childprocess(void)

{

int i;

for(i=1;i<=max count;i ++)

printf("from child,value=%d\n");

printf("child process done\n");

}

void parentprocess(void)

{

int i;

for(i=1;i<=max count;i ++)

printf("from child value=%d\n");

printf("child process done\n");

}

//using waitpid()

for(i=1:i<=max:i ++)

{

pid_t cpid = waitpid(pid[i] &stat, 0);

if(i=o,(stat))

printf("child %d with status%d\n, cpid(stat") )

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Please read the question carefully and in full and then answer it (using C language and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • Rewrite the program as a set of two programs that utilize a named pipe “ages” in...

    Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) {         int     fd[2], nbytes;         pid_t   childpid;         int users = 95;         char mystring[80];         char    readbuffer[80];         char digit1,digit2;         digit1 = (char) users%10;         digit2 = (char ) users/10;         mystring[0] = digit1;         mystring[1] = digit2;...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Modify the template program below and include the following additional functionalities: - Instead of 3 children...

    Modify the template program below and include the following additional functionalities: - Instead of 3 children processes, create 6 children processes. - 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, .... - Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167). - Keyboard inputs need to be monitored as in the original code. - Parent keeps reading from the...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • This is for a Unix class. Please fully answer the question :) The figure that it references is on...

    This is for a Unix class. Please fully answer the question :) The figure that it references is on the attached picture. I have also added the information that comes with that figure. 3.3 Assume that a process executes the following three function calls: fdl-open (path, oflags); fd2 = dup ( fd ) ; fd3-open (path, oflags); Draw the resulting picture, similar to Figure 3.9. Which descriptors are affected by an fentl on fdl with a command of F_SETFD? Which...

  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • Error: Unable to save the file in the folder...... Whats is wrong with this code.? The...

    Error: Unable to save the file in the folder...... Whats is wrong with this code.? The below code checks for the HTTP/1.1 200 OK. and if its true it divides tha pathname and hostname and after first slash in the pathname, it saves as the downloading link and have to save in the folder. but it 's not getting saved... int bytes sent; bytes sent send(sockfd, request strlen(request),0) int byte received; char buffer 10000J; bzero buffer 10000); //to make sure...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT