Systemcalls
Systemcalls
EXPERIMENT - 3
THREADS
Objective-
Q1)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
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>
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
print_file(argv[1]);
return 0;
}
#include <stdio.h>
#include <string.h>
int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
}
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;
}
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;
}
Q3)
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sched.h>
void child_process_function() {
exit(0);
}
int clone_function(void *arg) {
exit(0);
int main() {
if (pid == 0) {
exit(0);
wait(NULL);
pid = vfork();
if (pid == 0) {
exit(0);
wait(NULL);
if (pid == 0) {
exit(0);
pid = fork();
if (pid == 0) {
perror("exec failed");
exit(1);
wait(NULL);
return 0;
Output: