OS Assignment-1
Sangeeth Suresh
January 27, 2025
Student Details
• Class: S4 B2
• Roll Number: 68
Instructions
Develop a program in C to address the following scenario.
Questions
Question 1
There are 2 processes P1 and P2, both share an array of integers (shared memory). P1
accepts the array of integer and appends it to the shared array. Once P1 has appended
all the numbers into the array, P2 sorts the array in descending order. Once P2 finishes
sorting, P1 displays the sorted array in the terminal.
Answer:
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < unistd .h >
4 # include < sys / types .h >
5 # include < sys / wait .h >
6
7 # define MAX_SIZE 100
8
9 void sort_descending ( int * array , int size ) {
10 for ( int i = 0; i < size - 1; i ++) {
11 for ( int j = 0; j < size - i - 1; j ++) {
12 if ( array [ j ] < array [ j + 1]) {
13 int temp = array [ j ];
14 array [ j ] = array [ j + 1];
15 array [ j + 1] = temp ;
16 }
17 }
18 }
1
19 }
20
21 int main () {
22 int array [ MAX_SIZE ];
23 int size ;
24
25 printf ( " Enter the number of elements : " ) ;
26 scanf ( " % d " , & size ) ;
27
28 if ( size > MAX_SIZE ) {
29 printf ( " Maximum size exceeded . Limit is % d .\ n " , MAX_SIZE )
;
30 exit (1) ;
31 }
32
33 printf ( " Enter the elements : " ) ;
34 for ( int i = 0; i < size ; i ++) {
35 scanf ( " % d " , & array [ i ]) ;
36 }
37 printf ( " Elements appended ...\ n " ) ;
38 pid_t pid = fork () ;
39
40 if ( pid < 0) {
41 perror ( " fork " ) ;
42 exit (1) ;
43 } else if ( pid == 0) {
44 // Child process : Sort the array
45 printf ( " Child process sorting ...\ n " ) ;
46 sort_descending ( array , size ) ;
47 printf ( " Sorting complete \ n " ) ;
48
49 // Write the sorted array to a temporary file
50 FILE * temp_file = fopen ( " temp . txt " , " w " ) ;
51 if (! temp_file ) {
52 perror ( " fopen " ) ;
53 exit (1) ;
54 }
55 for ( int i = 0; i < size ; i ++) {
56 fprintf ( temp_file , " % d " , array [ i ]) ;
57 }
58 fclose ( temp_file ) ;
59 exit (0) ;
60 } else {
61 // Parent process waits for child to finish
62 wait ( NULL ) ;
63
64 // Read the sorted array from the temporary file
65 FILE * temp_file = fopen ( " temp . txt " , " r " ) ;
66 if (! temp_file ) {
67 perror ( " fopen " ) ;
68 exit (1) ;
2
69 }
70
71 printf ( " Sorted array in descending order : " ) ;
72 for ( int i = 0; i < size ; i ++) {
73 fscanf ( temp_file , " % d " , & array [ i ]) ;
74 printf ( " % d " , array [ i ]) ;
75 }
76 printf ( " \ n " ) ;
77 fclose ( temp_file ) ;
78
79 // Remove the temporary file
80 remove ( " temp . txt " ) ;
81 }
82
83 return 0;
84 }
Question 2
Implement a message queue where the sender sends a message to receiver receives the
message ,reverses it and retransmit to the sender. Sender has to print the reversed
message. This process is continued till “exit” is send as the message.
Answer:
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4 # include < sys / types .h >
5 # include < sys / wait .h >
6 # include < unistd .h >
7 # include < fcntl .h >
8 # include < sys / stat .h >
9
10 # define MAX_TEXT 100
11 # define TEMP_FILE " message_temp . txt "
12 # define FLAG_FILE " sync_flag . txt " // File for synchronization
13
14 void reverse_string ( char * str ) {
15 int len = strlen ( str ) ;
16 for ( int i = 0; i < len / 2; i ++) {
17 char temp = str [ i ];
18 str [ i ] = str [ len - i - 1];
19 str [ len - i - 1] = temp ;
20 }
21 }
22
23 void write_flag ( const char * flag ) {
24 FILE * flag_file = fopen ( FLAG_FILE , " w " ) ;
25 if (! flag_file ) {
26 perror ( " fopen " ) ;
27 exit (1) ;
3
28 }
29 fprintf ( flag_file , " % s " , flag ) ;
30 fclose ( flag_file ) ;
31 }
32
33 void wait_for_flag ( const char * expected_flag ) {
34 char flag [ MAX_TEXT ];
35 do {
36 FILE * flag_file = fopen ( FLAG_FILE , " r " ) ;
37 if (! flag_file ) {
38 perror ( " fopen " ) ;
39 exit (1) ;
40 }
41 fgets ( flag , MAX_TEXT , flag_file ) ;
42 fclose ( flag_file ) ;
43 flag [ strcspn ( flag , " \ n " ) ] = ’ \0 ’; // Remove newline
44 } while ( strcmp ( flag , expected_flag ) != 0) ;
45 }
46
47 int main () {
48 pid_t pid = fork () ;
49
50 if ( pid < 0) {
51 perror ( " fork " ) ;
52 exit (1) ;
53 } else if ( pid == 0) {
54 // Child process ( Receiver )
55 char message [ MAX_TEXT ];
56
57 while (1) {
58 // Wait for the parent to signal the message is ready
59 wait_for_flag ( " parent_done " ) ;
60
61 // Open the temporary file to read the message
62 FILE * file = fopen ( TEMP_FILE , " r " ) ;
63 if (! file ) {
64 perror ( " fopen " ) ;
65 exit (1) ;
66 }
67 fgets ( message , MAX_TEXT , file ) ;
68 fclose ( file ) ;
69
70 if ( strcmp ( message , " exit " ) == 0) {
71 break ;
72 }
73
74 // Reverse the message
75 reverse_string ( message ) ;
76
77 // Write the reversed message back to the temporary
file
4
78 file = fopen ( TEMP_FILE , " w " ) ;
79 if (! file ) {
80 perror ( " fopen " ) ;
81 exit (1) ;
82 }
83 fprintf ( file , " % s " , message ) ;
84 fclose ( file ) ;
85
86 // Signal the parent that the child is done
87 write_flag ( " child_done " ) ;
88 }
89
90 exit (0) ;
91 } else {
92 // Parent process ( Sender )
93 char message [ MAX_TEXT ];
94
95 // Initialize the flag file
96 write_flag ( " child_done " ) ;
97
98 while (1) {
99 printf ( " Enter a message ( or ’ exit ’ to quit ) : " ) ;
100 fgets ( message , MAX_TEXT , stdin ) ;
101 message [ strcspn ( message , " \ n " ) ] = ’ \0 ’; // Remove
newline character
102
103 // Write the message to the temporary file
104 FILE * file = fopen ( TEMP_FILE , " w " ) ;
105 if (! file ) {
106 perror ( " fopen " ) ;
107 exit (1) ;
108 }
109 fprintf ( file , " % s " , message ) ;
110 fclose ( file ) ;
111
112 // Signal the child that the message is ready
113 write_flag ( " parent_done " ) ;
114
115 if ( strcmp ( message , " exit " ) == 0) {
116 break ;
117 }
118
119 // Wait for the child to signal it ’s done processing
120 wait_for_flag ( " child_done " ) ;
121
122 // Read the reversed message from the temporary file
123 file = fopen ( TEMP_FILE , " r " ) ;
124 if (! file ) {
125 perror ( " fopen " ) ;
126 exit (1) ;
127 }
5
128 fgets ( message , MAX_TEXT , file ) ;
129 fclose ( file ) ;
130
131 printf ( " Reversed message : % s \ n " , message ) ;
132 }
133
134 wait ( NULL ) ; // Wait for child process to finish
135
136 // Remove the temporary files
137 remove ( TEMP_FILE ) ;
138 remove ( FLAG_FILE ) ;
139 }
140
141 return 0;
142 }