Professional Documents
Culture Documents
Lab 1
Lab 1
Lab Report-1
Section 1
Output:
text.txt:
2) Write a program to read the first 10 characters of the file text. (attach two
different outputs 1) O_RDONLY 2) O_WRONLY)
3) Write a program which reads numbers from an input file and performs addition
of the numbers.(use dup2()).
numbers.txt:
Explanation: A file ‘numbers.txt’ contains numbers. The file is opened. Sum is calculated using
for loop and is printed. We uses open system call to open the file and use dup2() to redirect the
file descriptor to stdin.
Output:
Exercise 2
1) See the code
int main() {
int i, nchildren = 0;
pid_t pid;
for (i = 1; i <= 2; i++) {
pid = fork();
if (pid > 0)
nchildren++;
}
printf("I had %d children\n", nchildren);
return nchildren;
}
(a) How many new processes (in addition to the initial process) are created by
this code? Draw a process graph that illustrates the processes at run-time.
Ans:
In this code, fork() creates three processes.
So we have total 4 processes, 1 parent and 3 child processes.
Initial(first) Process is main() function.
The first call to fork() creates “Child Process 1”
The second call to fork() creates “Child Process 2”.
Child process 1 further creates “Child process 3”.
(b) I executed this program once and generated the following
I had 0 children
I had 2 children
I had 1 children
Adding the numbers suggests that 4 child processes were created. Explain why
Ans:
(b)
Output depends on the processor it may give different output all the time.
Because a process which comes at the last statement will print that statement,we
can not say which process reaches the end first
.
2) How many total processes and child processes will be created in the following
code?
void main()
{
int i, n;
for ( i=1; i<=n; i++ )
fork();
}
Ans:
Total processes: 2^n
Child processes: 2^n -1
3) What will be the value of the a, b, c? Explain it with the proper diagram
void main(){
int a=1, b=2, c=3;
c+=a+=b+=c*2;
print(a,b,c);
if(fork() == 0){
a+=b+=c+=3;
print(a,b,c);
c++;
}
else{
b*=a*=c+=3;
print(a,b,c);
c--;
}
c+=a+=b+1;
print(c);
}
4) Predict the output for the following code and also illustrate it using a graph.
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid;
float a, b, c;
pid = fork();
if (pid == 0)
a=1+1;
else
wait();
b=2+2;
c=a+b;
printf("c=%f\n", c);
return 0;
}
Exercise 3
1) Write a program: which runs two processes. The parent process prints all the
destination IPs and the child process prints a list of all files and directories under
the same directory in which C program.
Explanation: In this code we use fork() system call to create a child process ,after
this we have used if-else condition.Child process prints list of all file is same
directory using ‘execlp’ function and Parent process print destination IPs.
Output:
2) Write a program: The child process should create an empty file. After
successful creation of an empty file, the parent process should append “This
line is appended by the parent process” to the empty file using file I/O syscalls.
Explanation:
In this code i use fork() function to create child process .After this child process
creates a file input.txt.And the parent process append “This line is appended by
the parent process”
newFile.txt
Output:
Section 4
#define NUM_PROCESSES 4
char data_buffer[256];
exit(0);
}
int main()
{
int read_pipes[NUM_PROCESSES][2];
FILE *input_files[NUM_PROCESSES];
FILE *output_files[NUM_PROCESSES];
pid_t child_process_pids[NUM_PROCESSES];
fd_set read_fd_set;
int max_fd = -1;
char input_filename[10];
char output_filename[10];
snprintf(input_filename, sizeof(input_filename), "i%d.txt", i + 1);
snprintf(output_filename, sizeof(output_filename), "o%d.txt", i + 1);
input_files[i] = fopen(input_filename, "r");
output_files[i] = fopen(output_filename, "w");
if (!input_files[i] || !output_files[i])
{
perror("File opening failed");
return 1;
}
while (1)
{
FD_ZERO(&read_fd_set);
for (int i = 0; i < NUM_PROCESSES; ++i)
{
FD_SET(fileno(input_files[i]), &read_fd_set);
}
return 0;
}
Explanation:
In this code we created one parent and four child process .Where parent and
childrens pass messages to each other. The parent receives messages from
● Four pipes are created,between parents and childs.,the parent and each
● Each child reads a message from its own file ,then sends it to the parent.
● The Parent waits for messages from any child using the select() system
Output: