The code is followed by the output snapshot
/* File: hw3q4.c */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buf[1024];
int buflength;
int s1[4], s2[4];
int sr[8], srsize; /* result */
int fd1, fd2, fd3; /* file descriptors */
pid_t pid;
int i, j, k;
int status;
char * s;
/* check for command line arguments */
if(argc < 4)
{
sprintf(buf, "Usage: %s
<file1> <file2> <file3>\n", argv[0]);
buflength =
strlen(buf);
write(1, buf,
buflength);
return -1;
}
/* open fds and check for errors */
if((fd1 = open(argv[1], O_RDONLY)) < 0)
{
sprintf(buf, "Error
opening file: %s\n", argv[1]);
buflength =
strlen(buf);
write(1, buf,
buflength);
return -2;
}
if((fd2 = open(argv[2], O_RDONLY)) <
0)
{
sprintf(buf, "Error
opening file: %s\n", argv[2]);
buflength =
strlen(buf);
write(1, buf,
buflength);
close(fd1);
return -2;
}
/* create with read, write permissions
*/
if((fd3 = open(argv[3], O_CREAT | O_TRUNC |
O_WRONLY, S_IRUSR | S_IWUSR)) < 0)
{
sprintf(buf, "Error
opening file: %s\n", argv[3]);
buflength =
strlen(buf);
write(1, buf,
buflength);
close(fd1);
close(fd2);
return -3;
}
/* read the file contents */
buflength = read(fd1, buf, 1024);
for(i = 0, s = buf; i < 4; i++)
{
/* parse an integer
*/
s1[i] = 0;
while(*s != ' '
&& buflength > 1)
{
s1[i] = s1[i] * 10 + (*s - '0');
s++;
buflength --;
}
while(*s == ' '
&& buflength > 1)
{
s++; /* skip the whitespace */
buflength --;
}
/* printf("Integer
%d: %d\n", i + 1, s1[i]); */
}
buflength = read(fd2, buf, 1024);
for(i = 0, s = buf; i < 4; i++)
{
/* parse an integer
*/
s2[i] = 0;
while(*s != ' '
&& buflength > 1)
{
s2[i] = s2[i] * 10 + (*s - '0');
s++;
buflength --;
}
s++; /* skip the
whitespace */
buflength --;
/* printf("Integer
%d: %d\n", i + 1, s2[i]); */
}
/* fork a child */
pid = fork();
if(pid == 0) /* child */
{
/* intersection */
/* printf("Child\n");
*/
i = j = k = srsize =
0;
while(i < 4
&& j < 4)
{
if(s1[i] == s2[j])
{
sr[k++] = s1[i];
i++;
j++;
}
else if(s1[i] < s2[j])
{
i++;
}
else
{
j++;
}
}
srsize = k;
sprintf(buf, "Child
process PID: %d Intersection of {", getpid());
}
else /* parent */
{
/* printf("Parent\n");
*/
/* union */
i = j = k = srsize =
0;
while(i < 4
&& j < 4)
{
if(s1[i] == s2[j])
{
sr[k++] = s1[i];
i++;
j++;
}
else if(s1[i] < s2[j])
{
sr[k++] = s1[i++];
}
else
{
sr[k++] = s2[j++];
}
}
while(i < 4)
{
sr[k++] = s1[i++];
}
while(j < 4)
{
sr[k++] = s2[j++];
}
srsize = k;
sprintf(buf, "Parent
process PID: %d Intersection of {", getpid());
}
/* Prepare the text for writing */
for(i = 0; i < 4; i++)
{
if(i == 0)
sprintf(buf, "%s%d", buf, s1[i]);
else
sprintf(buf, "%s,%d", buf, s1[i]);
}
sprintf(buf, "%s} and {", buf);
for(i = 0; i < 4; i++)
{
if(i == 0)
sprintf(buf, "%s%d", buf, s2[i]);
else
sprintf(buf, "%s,%d", buf, s2[i]);
}
sprintf(buf, "%s} = {", buf);
for(i = 0; i < srsize; i++)
{
if(i == 0)
sprintf(buf, "%s%d", buf, sr[i]);
else
sprintf(buf, "%s,%d", buf, sr[i]);
}
sprintf(buf, "%s}\n", buf);
buflength = strlen(buf);
if(pid == 0) /* child */
{
/* write immediately
*/
write(fd3, buf,
buflength);
/* printf("Child
done\n"); */
}
else
{
/* wait for child to
finish */
wait(&status);
/* write to file
*/
write(fd3, buf,
buflength);
/* printf("Parent
done\n"); */
}
/* close fds */
close(fd1);
close(fd2);
close(fd3);
return 0;
}
Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...
Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the fork function to produce two processes. In the child process print out the lower case version of the string. In the parent process print out the reversed upper case version of the string (e.g. child: “abc”; parent: ”CBA”). You may call the toupper and tolower functions in the header <ctype.h> if you wish. Specify in the output whether the parent or child process is...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit...
In C write a program that must accept one argument from the command line. The argument is the name of the file containing the processes (for example: processes.csv).This file is comma-separated with three columns (process ID, arrival time and burst time) with each row for an individual process. You can assume that this file will have a maximum of 10 processes. For example Input: processes.csv ProcessID,Arrival Time,Burst Time 0,1,3 1,0,5 2,9,8 3,10,6 Where the first element is processes, the second...
Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance, >sortit 3 5 7 >The sorted list is 3, 5, 7. >sortit 4 0 9 >The sorted list is 0, 4, 9 >sortit 1 p 9 >sortit: invalid input p. >sortit >usage: sortit num1 num2 num3! 1. The source code of a c file 2. Screenshots that show successful execution...
Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...
Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...
10) Unlike a signal, which conveys only the occurrence of a particular event and contains no information content, a pipe can be thought of as a scratch file created by a system call. It can be used as a communications channel between concurrently running processes. The interface call to a pipe is similar to that for any file. In fact, the process reads and writes to a pipe just like any file. Unlike files, however, pipes do not represent actual...