ASSESSMENT 1 Lab
ASSESSMENT 1 Lab
SUBMITTED TO:
MA’AM SABA
SUBMITTED BY:
M HAMZA SAJJAD
REGISTRATION NO:
UW-22-AI-BS-042
COURSE TITLE:
OPERATING SYSTEM
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;
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;
}