Question

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;

        printf("%d %d\n",mystring[0],mystring[1]);

        pipe(fd);

        if((childpid = fork()) == -1)

        {

                perror("fork");

                exit(1);

        }

        if(childpid == 0)

        {

                /* Child process closes up input side of pipe */

                close(fd[0]);

                /* Send "string" through the output side of pipe */

                write(fd[1], mystring, sizeof(mystring));

                exit(0);

        }

        else

        {

                /* Parent process closes up output side of pipe */

                close(fd[1]);

                /* Read in a string from the pipe */

                nbytes = read(fd[0], readbuffer, sizeof(readbuffer));

                printf("Received string: %d %d\n",readbuffer[0],readbuffer[1]);

        }

}

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

// C program to implement one side of FIFO
// This side writes
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   int fd;
int users = 95;
char mystring[80];
char digit1,digit2;
digit1 = (char) users%10;
digit2 = (char ) users/10;
mystring[0] = digit1;
mystring[1] = digit2;
mknod("/ages", S_IFIFO|0666, 0); // Creating the named file(FIFO)
fd = open(myfifo, O_WRONLY); // Open FIFO for write only
write(fd,mystring,sizeof(mystring));
   close(fd);
   return 0;
}
.

.

// C program to implement second side of FIFO
// This side reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   int fd;
char readbuffer[80];
fd = open("/ages",O_RDONLY); // Open FIFO for read only
read(fd,readbuffer,sizeof(readbuffer));
printf("Received string: %d %d\n",readbuffer[0],readbuffer[1]);
   close(fd);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Rewrite the program as a set of two programs that utilize a named pipe “ages” in...
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
  • 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...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • /* 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...

  • 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   ...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • Please read the question carefully and in full and then answer it (using C language and...

    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). 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()...

  • 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...

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