Modify this code to let it run concurrently to send and receive any messages between 2 processes and take one command line argument to change between 2 processes, for example, 1 or 2 to indicate the process 1 or process 2. #include #include #include #include #include #include #include #define FIFO_NAME “/tmp/my_ fo” int main(int argc, char *argv[]) { int res, open_mode = 0; if (argc < 2) { fprintf(stderr, “Usage: %s <[O_RDONLY, O_WRONLY, O_RDWR, O_NONBLOCK]>\n”, *argv); exit(EXIT_FAILURE); } argv++; if (*argv) { if (strncmp(*argv, “O_RDONLY”, 8) == 0) open_mode |= O_RDONLY; if (strncmp(*argv, “O_WRONLY”, 8) == 0) open_mode |= O_WRONLY; if (strncmp(*argv, “O_RDWR”, 6) == 0) open_mode |= O_RDWR; if (strncmp(*argv, “O_NONBLOCK”, 10) == 0) open_mode |= O_NONBLOCK; } if (access(FIFO_NAME, F_OK) == -1 { res = mk fo(FIFO_NAME, 0777); if (res) { fprintf(stderr, “Could not create fo %s\n”, FIFO_NAME); exit(EXIT_FAILURE); } } printf(“Process %d opening FIFO\n”, getpid()); res = open(FIFO_NAME, open_mode); printf(“Process %d result %d\n”, getpid(), res); sleep(5); if (res != -1) (void) close(res); printf(“Process %d nished\n”, getpid()); unlink(FIFO_NAME); exit(EXIT_SUCCESS); }
Modify this code to let it run concurrently to send and receive any messages between 2 processes and take one command line argument to change between 2 processes, for example, 1 or 2 to indicate the process 1 or process 2.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME “/tmp/my_ fo”
int main(int argc, char *argv[])
{
int res, open_mode = 0;
if (argc < 2)
{
fprintf(stderr, “Usage: %s <[O_RDONLY,
O_WRONLY, O_RDWR, O_NONBLOCK]>\n”, *argv);
exit(EXIT_FAILURE);
}
argv++;
if (*argv)
{
if (strncmp(*argv, “O_RDONLY”, 8) == 0)
open_mode |= O_RDONLY;
if (strncmp(*argv, “O_WRONLY”, 8) == 0)
open_mode |= O_WRONLY;
if (strncmp(*argv, “O_RDWR”, 6) == 0)
open_mode |= O_RDWR;
if (strncmp(*argv, “O_NONBLOCK”, 10) == 0)
open_mode |= O_NONBLOCK;
}
if (access(FIFO_NAME, F_OK) == -1
{
res = mk fo(FIFO_NAME, 0777);
if (res)
{
fprintf(stderr, “Could not create fo
%s\n”, FIFO_NAME);
exit(EXIT_FAILURE);
}
}
printf(“Process %d opening FIFO\n”, getpid());
res = open(FIFO_NAME, open_mode);
printf(“Process %d result %d\n”, getpid(), res);
sleep(5);
if (res != -1) (void) close(res);
printf(“Process %d nished\n”, getpid());
unlink(FIFO_NAME);
exit(EXIT_SUCCESS);
}
Step by step
Solved in 2 steps