0% found this document useful (0 votes)
9 views9 pages

ASSESSMENT 1 Lab

Uploaded by

hamzasajjad293
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)
9 views9 pages

ASSESSMENT 1 Lab

Uploaded by

hamzasajjad293
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/ 9

Sessional # 1

SUBMITTED TO:
MA’AM SABA
SUBMITTED BY:
M HAMZA SAJJAD
REGISTRATION NO:
UW-22-AI-BS-042
COURSE TITLE:
OPERATING SYSTEM

DEPARTMENT OF COMPUTER SCIENCE ( AI )


UNIVERSITY OF WAH
Question No 01:
#include <stdio.h>
#include <stdlib.h>
int main() {
int count;
printf("Enter number of files to create: ");
scanf("%d", &count);
for (int i = 0; i < count; i++) {
char filename[20];
sprintf(filename, "file_%d.txt", i);
FILE *file = fopen(filename, "w");
if (file) {
fprintf(file, "This is file %d\n", i);
fclose(file);
}
file = fopen(filename, "r");
if (file) {
char buffer[100];
printf("Contents of %s: ", filename);
while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer);
}
fclose(file);
}
remove(filename);
printf("Deleted: %s\n", filename);
}
return 0;
}

b) int main() {
>> long res = syscall(SYS_getpid); // Using syscall to get the process ID
>> printf("Process ID using syscall: %ld\n", res); // Printing the process ID
>> return 0;

c)
#include <stdio.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <unistd.h>
int main() {
int input_fd = open("source.txt", O_RDONLY);
int output_fd = open("destination.txt", O_WRONLY | O_CREAT, 0644);
if (input_fd < 0 || output_fd < 0) {
perror("Failed to open files");
return 1;
}
off_t offset = 0;
ssize_t bytes_copied = sendfile(output_fd, input_fd, &offset, 1024);
if (bytes_copied == -1) {
perror("Error during sendfile");
close(input_fd);

close(output_fd);
return 1;
} printf("Bytes copied: %zd\n", bytes_copied); // Use %zd for ssize_t
close(input_fd);
close(output_fd)
return 0;
}
Question no 2:
a)

#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*) malloc(sizeof(int) * 5);
if (ptr == NULL) {
perror("Memory allocation failed");
return 1;
}
for (int i = 0; i < 5; ++i) {
ptr[i] = i * 10;
}
for (int i = 0; i < 5; ++i) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}
-
b)
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

int main() {
struct stat fileStat;

if (stat("example.txt", &fileStat) < 0) {


perror("Stat failed");
return 1;
}

printf("File size: %ld bytes\n", fileStat.st_size);


printf("Last access: %s", ctime(&fileStat.st_atime));
return 0;
}

c)
#include <pthread.h>
#include <stdio.h>
void *fuc(void *arg)
{
printf("Inside the thread\n");
pthread_exit(NULL);
}
void fun()
{
pthread_t ptid;
if (pthread_create(&ptid, NULL, &fuc, NULL) != 0) {
perror("pthread_create failed");
return;
}
printf("This line is printed before thread termination\n");
if (pthread_equal(ptid, pthread_self())) {
printf("Threads are equal\n");
} else {
printf("Threads are not equal\n");
}
pthread_join(ptid, NULL);
printf("This line will be printed after thread ends\n");
int main()
{
fun();
return 0;
}

QUESTION N0 03:
b)
#include <stdio.h>
#include <pthread.h>
void* add(void* arg) {
int* nums = (int*)arg;
printf("Addition: %d + %d = %d\n", nums[0], nums[1], nums[0] + nums[1]);
return NULL;
}
void* subtract(void* arg) {
int* nums = (int*)arg;
printf("Subtraction: %d - %d = %d\n", nums[0], nums[1], nums[0] - nums[1]);
return NULL;
}
void* multiply(void* arg) {
int* nums = (int*)arg;
printf("Multiplication: %d * %d = %d\n", nums[0], nums[1], nums[0] * nums[1]);
return NULL;
}
void* divide(void* arg) {
int* nums = (int*)arg;
if (nums[1] != 0) {
printf("Division: %d / %d = %d\n", nums[0], nums[1], nums[0] / nums[1]);
} else {
printf("Division: Division by zero error!\n");
}
return NULL;
}
int main() {
pthread_t thread1, thread2, thread3, thread4;
int nums[] = {10, 5};
pthread_create(&thread1, NULL, add, (void*)nums);
pthread_create(&thread2, NULL, subtract, (void*)nums);
pthread_create(&thread3, NULL, multiply, (void*)nums);
pthread_create(&thread4, NULL, divide, (void*)nums);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
return 0;
}

You might also like