Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Advanced Operating Systems

Lab Project

1. Write a C- program that do the following: 2 marks


- Ask the user to enter a number.
- Print out all the odd numbers that bellow the entering number.

2. Read the given algorithm carefully and write a C- program to simulate


the system call. 4
marks
Algorithm:
- Create a new child using fork () system call.
- Check for error condition and display error message.
- If return value of fork is not equal to 0 and display it all parent and getpid
using getpid () system call.
- If return value of fork equal to 0 then display it as child process and getpid ()
system call.
- Print the corresponding result.

3. Write a C- program that do the following: 4 marks


- Create a text file (name it as OSLAB) and add your first and last name as
content of the file.
- Use the C program file to copy the content of the file (OSLAB) to another file.
- Print out the file size of the source file.
- Print out the file size of destination file.

Answer:
Matrea Ahmed
ID201912758
1-

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
void print_odd_numbers(int number) {
printf("Odd numbers below %d:\n", number);
int i = 1;
while (i < number) {
printf("%d ", i);
i += 2;
}
printf("\n");
}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

print_odd_numbers(number);

return 0;
}
2-

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
pid_t child_pid = fork();
if (child_pid == -1) {
perror("Fork error");
exit(1);
} else if (child_pid == 0) {
printf("Child process - PID: %d\n", getpid());
} else {
wait(NULL);
printf("Parent process - PID: %d\n", getpid());
}

return 0;
}

3-
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *source_file, *destination_file;
char ch;
// Create the source file and write content
source_file = fopen("OSLAB.txt", "w");
if (source_file == NULL) {
printf("Error creating source file.\n");
exit(1);
}
fprintf(source_file, "First Name: Matrea\nLast Name: Ahmed\n");
fclose(source_file);

// Open the source file for reading


source_file = fopen("OSLAB.txt", "r");
if (source_file == NULL) {
printf("Error opening source file.\n");
exit(1);
}

// Open the destination file for writing


destination_file = fopen("OSLAB_copy.txt", "w");
if (destination_file == NULL) {
printf("Error creating destination file.\n");
exit(1);
}

// Copy content from source to destination


while ((ch = fgetc(source_file)) != EOF) {
fputc(ch, destination_file);
}

// Close the files


fclose(source_file);
fclose(destination_file);

// Get file sizes


long source_size, destination_size;
source_file = fopen("OSLAB.txt", "r");
fseek(source_file, 0, SEEK_END);
source_size = ftell(source_file);
fclose(source_file);

destination_file = fopen("OSLAB_copy.txt", "r");


fseek(destination_file, 0, SEEK_END);
destination_size = ftell(destination_file);
fclose(destination_file);

// Print file sizes


printf("Source file size: %ld bytes\n", source_size);
printf("Destination file size: %ld bytes\n", destination_size);

return 0;
0;
}

You might also like