OPERATING SYSTEMS LAB (BCSE303P)
EXPERIMENT - 3
THREADS
Name- Utkarsh Varman
Register No- 21BLC1030
Aim: To Study about Threads
Objective-
Q1)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void printRow(int num, int pid, int i) {
printf("%d----%d\n", pid, num * i);
}
int main() {
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork Failed");
return 1;
} else if (pid == 0) {
for (int i = 1; i <= 10; i++) {
printRow(5, 1423, i);
usleep(50000);
}
} else {
for (int i = 1; i <= 10; i++) {
printRow(2, 1323, i);
usleep(50000);
}
wait(&status);
}
return 0;
}
Output:
Q2)
#include <stdio.h>
#include <string.h>
void print_file(const char* filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Unable to open file %s\n", filename);
return;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
// Close the file
fclose(file);
}
int main(int argc, char* argv[])
{
FILE* file;
char ch;
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
print_file(argv[1]);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to print the contents of a file
void print_file(const char* filename)
{
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Unable to open file %s\n", filename);
return;
}
int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
}
// Function to write input from the user to a file
void write_to_file(const char* filename)
{
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("Unable to open file %s\n", filename);
return;
}
char buffer[512];
fgets(buffer, 512, stdin);
fputs(buffer, file);
fclose(file);
}
// Function to concatenate the contents of two files
void concatenate_files(const char* filename1,
const char* filename2)
{
FILE* file1 = fopen(filename1, "r+");
if (file1 == NULL) {
printf("Unable to open file %s\n", filename1);
return;
}
FILE* file2 = fopen(filename2, "r");
if (file2 == NULL) {
printf("Unable to open file %s\n", filename2);
fclose(file1);
return;
}
// Move file pointer to the end of the first file
fseek(file1, 0, SEEK_END);
int ch;
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, file1);
}
fclose(file1);
fclose(file2);
}
// driver code
int main(int argc, char* argv[])
{
if (argc < 2) {
// Print usage information if no filenames are
// provided
printf("Usage: %s filename1 [filename2 ...]\n",
argv[0]);
return 1;
}
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-") == 0) {
// If the argument is "-", write to the
// specified file
write_to_file(argv[++i]);
}
else if (i + 1 < argc
&& strcmp(argv[i + 1], "-") == 0) {
// If the next argument is "-", concatenate the
// two specified files
concatenate_files(argv[i], argv[i + 2]);
i += 2;
}
else {
// Print the contents of the file
printf("%s :\n", argv[i]);
print_file(argv[i]);
printf("\n");
}
}
return 0;
}
Output:
Q3)
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sched.h>
void child_process_function() {
printf("Child process with PID %d\n", getpid());
exit(0);
}
int clone_function(void *arg) {
printf("Child process created using clone with PID %d\n", getpid());
exit(0);
int main() {
pid_t pid = fork();
if (pid == 0) {
printf("Child process (fork) with PID %d\n", getpid());
exit(0);
} else if (pid > 0) {
wait(NULL);
pid = vfork();
if (pid == 0) {
printf("Child process (vfork) with PID %d\n", getpid());
exit(0);
} else if (pid > 0) {
wait(NULL);
char clone_stack[1024 * 1024];
pid = clone(clone_function, clone_stack + sizeof(clone_stack), CLONE_VM | CLONE_FS |
CLONE_FILES, NULL);
if (pid == 0) {
printf("Child process (clone) with PID %d\n", getpid());
exit(0);
} else if (pid > 0) {
wait(NULL);
pid = fork();
if (pid == 0) {
printf("Child process before exec with PID %d\n", getpid());
execlp("/bin/ls", "ls", NULL);
perror("exec failed");
exit(1);
} else if (pid > 0) {
wait(NULL);
return 0;
Output: