Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

MC214 Operating Systems

Lab Report-1

Student1: Krushi Sutariya(202203044)


Student2: Heer Dharsandiya(202203059)
Student3: Rutvi Chauhan(202203014)

Section 1

1) Continuing Example: 1, write a C program to append - “This is appended line.”


to the same file. And fetch the appended statement from the open file.
Explanation: ‘text.txt’ file is opened using ‘open’. The string ‘a’ is written in the file. Then the
string ‘b’ is written in the file. The cursor is shifted at the beginning of string ‘b’ using ‘lseek’.
Then the appended string is read and printed.

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)

1) Explanation: ‘input.txt’ file is already created. We create a string of length 11 to


accommodate all 10 characters and null character. Then 10 characters are read using
‘read’ and then printed.
Input.txt

2) Explanation: A file ‘output.txt’ is created and content is printed in it.


Output.txt:

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

output. I had 1 children

I had 0 children

I had 2 children

I had 1 children

Adding the numbers suggests that 4 child processes were created. Explain why

this differs from your answer for part (a).

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

Chat Program using Select, IPC, and Pipes


1. Parent process serves as the server, monitoring using select.
2. Four child processes are created using fork, each as a client.
3. Input files (i1.txt, i2.txt, i3.txt, i4.txt) and output files (o1.txt, o2.txt, o3.txt,
o4.txt) are used for data exchange.
4. Two-way pipes enable communication between parent and children.
5. Child processes read first lines from inputs and write to outputs.
a. Child 1 sends to Child 2-4 via pipes.
b. Each child receives others' messages and writes to its output.
The program showcases inter-process communication, employing pipes, file
descriptors, and select system calls. Parent-server oversees chat among four
child-client processes, utilizing input/output files for messaging while
maintaining message exchange between children.
Ensure that the input files (i1.txt, i2.txt, etc.) are present in the same directory as
the program.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>

#define NUM_PROCESSES 4

void process_child(int process_id, int read_pipe[], FILE *output_files[])


{

char data_buffer[256];

read(read_pipe[process_id - 1], data_buffer, sizeof(data_buffer));


printf("Process %d received: %s", process_id, data_buffer);

fprintf(output_files[process_id - 1], "Process %d received: %s", process_id,


data_buffer);

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;

// Pipe and file setup


for (int i = 0; i < NUM_PROCESSES; ++i)
{
if (pipe(read_pipes[i]) == -1)
{
perror("Pipe creation failed");
return 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;
}

if (fileno(input_files[i]) > max_fd)


{ max_fd = fileno(input_files[i]);
}
}
for (int i = 0; i < NUM_PROCESSES; ++i)
{
child_process_pids[i] = fork();
if (child_process_pids[i] == 0)
{
close(read_pipes[i][0]);
process_child(i + 1, read_pipes[i], output_files);
} else if (child_process_pids[i] < 0) {
perror("Fork failed");
return 1;
}
}

for (int i = 0; i < NUM_PROCESSES; ++i)


{
if (fileno(input_files[i]) > max_fd)
{
max_fd = fileno(input_files[i]);
}
}

while (1)
{
FD_ZERO(&read_fd_set);
for (int i = 0; i < NUM_PROCESSES; ++i)
{
FD_SET(fileno(input_files[i]), &read_fd_set);
}

int ready = select(max_fd + 1, &read_fd_set, NULL, NULL, NULL);


if (ready == -1)
{
perror("Select failed");
break;
}

for (int i = 0; i < NUM_PROCESSES; ++i)


{
if (FD_ISSET(fileno(input_files[i]), &read_fd_set))
{
char data_buffer[256];
if (fgets(data_buffer, sizeof(data_buffer), input_files[i]) != NULL)
{
write(read_pipes[i][1], data_buffer, strlen(data_buffer) + 1);
}
}
}
}

for (int i = 0; i < NUM_PROCESSES; ++i)


{

waitpid(child_process_pids[i], NULL, 0);


fclose(input_files[i]);
fclose(output_files[i]);
close(read_pipes[i][1]);
}

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

children and forwards them to the next child in line.

● Four pipes are created,between parents and childs.,the parent and each

child have a pipe for sending and receiving messages.

● 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

call.When a message arrives from a child,the parent reads it and forwards

it to the next child.

Output:

You might also like