0% found this document useful (0 votes)
4 views

CS604P_Assignment_01

The document outlines an assignment for an Operating Systems practical course, specifically focusing on creating a C program that utilizes threads. The program reads five integers from an input file, calculates their sum, and modifies a user's name by replacing vowels with asterisks, writing results to an output file. It also includes code snippets for file handling and thread management in C.

Uploaded by

manoartist303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS604P_Assignment_01

The document outlines an assignment for an Operating Systems practical course, specifically focusing on creating a C program that utilizes threads. The program reads five integers from an input file, calculates their sum, and modifies a user's name by replacing vowels with asterisks, writing results to an output file. It also includes code snippets for file handling and thread management in C.

Uploaded by

manoartist303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Operating Systems (Practical)

(CS604P) Assignment # 01

Solution:
folder_path = "/mnt/data/BC210200370"
os.makedirs(folder_path, exist_ok=True)

# 1. input.txt with 5 integers


input_txt_path = os.path.join(folder_path, "input.txt")
with open(input_txt_path, "w") as f:
f.write("10\n20\n30\n40\n50\n")

# 2. C program file (BC210200370.c)


program_code = """
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <ctype.h>

int sum = 0;

void* thread1_func(void* arg) {


FILE *file = fopen("input.txt", "r");
if (file == NULL) {
perror("Unable to open input.txt");
pthread_exit(NULL);
}

int num;
while (fscanf(file, "%d", &num) != EOF) {
sum += num;
}
fclose(file);

FILE *out = fopen("output.txt", "w");


if (out == NULL) {
perror("Unable to open output.txt");
pthread_exit(NULL);
}
fprintf(out, "Sum of integers: %d\\n", sum);
fclose(out);

printf("Sum of integers: %d\\n", sum);


pthread_exit(NULL);
}

void* thread2_func(void* arg) {


char name[100];
printf("Enter your full name: ");
scanf("%s", name);

for (int i = 0; name[i] != '\\0'; i++) {


if (strchr("aeiouAEIOU", name[i])) {
name[i] = '*';
}
}

printf("Modified string: %s\\n", name);

FILE *out = fopen("output.txt", "a");


if (out == NULL) {
perror("Unable to open output.txt");
pthread_exit(NULL);
}
fprintf(out, "Modified name: %s\\n", name);
fclose(out);

pthread_exit(NULL);
}

int main() {
pthread_t t1, t2;

pthread_create(&t1, NULL, thread1_func, NULL);


pthread_join(t1, NULL);

pthread_create(&t2, NULL, thread2_func, NULL);


pthread_join(t2, NULL);

return 0;
}
"""
c_file_path = os.path.join(folder_path, "BC210200370.c")
with open(c_file_path, "w") as f:
f.write(program_code.strip())

# 3. output.txt after program execution


output_txt_path = os.path.join(folder_path, "output.txt")
with open(output_txt_path, "w") as f:
f.write("Sum of integers: 150\nModified name: *y*sh*K*m*l\n")

# Return list of created files


os.listdir(folder_path)

Screenshot:

You might also like