0% found this document useful (0 votes)
3 views

Lab Report 3 - OS

The document is a lab report focused on multiple processes programming in Linux, detailing the implementation of various tasks using system calls. It includes code examples for creating child processes, waiting for process state changes, executing files, and verifying if numbers are growing, prime, or Fibonacci numbers using multi-process techniques. Each section provides code snippets, explanations, and expected outputs.

Uploaded by

Nguyễn Lộc
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)
3 views

Lab Report 3 - OS

The document is a lab report focused on multiple processes programming in Linux, detailing the implementation of various tasks using system calls. It includes code examples for creating child processes, waiting for process state changes, executing files, and verifying if numbers are growing, prime, or Fibonacci numbers using multi-process techniques. Each section provides code snippets, explanations, and expected outputs.

Uploaded by

Nguyễn Lộc
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/ 11

NAME: NGUYỄN VĂN LỘC

ID: ITITIU20244

LAB REPORT 3

This session aims at revising multiple processes programming on Linux. The focus is on
implement-
ing programs to do some particular tasks using system calls.

Problem 2.1: Process and process control library in Linux

● Create a child process

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

int main() {
pid_t pid = fork();
if(pid < 0) {
perror("fork: Cannot create new process");
exit(1);
} else if (pid == 0) {
printf("Child process created! %d\n", getpid());
} else {
printf("Parent process created! %d\n", getpid());
}
return 0;
}
Output:

● Wait for a process to change state

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *status, int options);
int main() {
pid_t pid = fork();
if(pid < 0) {
perror("fork: Cannot create new process");
exit(1);
} else if (pid == 0) {
printf("Child process created! %d\n", getpid());
} else {
printf("Parent process created! %d\n", getpid());
waitpid(pid, NULL, 0);
}
return 0;
}
Screenshot:

Explain:
Wait for the child process. The child process will become an orphan
process, if the parent process is terminated.

● Execute a file

● Code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
pid_t pid = fork();
if (pid < 0) {
perror("fork: cannot create a new process");
exit(EXIT_FAILURE);
} else if (pid == 0) {
/* Child process code */
printf("Child process ID: %d\n", getpid());

// Execute a file in the child process


char *const args[] = {"ls", "-l", NULL};
execvp("ls", args);

// If execvp() returns, it indicates an error occurred


perror("execvp");
exit(EXIT_FAILURE);
} else {
/* Parent process code */
printf("Parent process ID: %d\n", getpid());

// Wait for the child process to terminate


int status;
waitpid(pid, &status, 0);

if (WIFEXITED(status)) {
printf("Child process exited with status %d\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("Child process terminated by signal %d\n",
WTERMSIG(status));
}
}
return 0;
}

● Screenshot
Problem 2.2: Multi-process Growing program

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdbool.h>

int isGrowingNumber(int num)


{ if(num < 10)
return 1;

int lastDigit = num % 10;


num /= 10;

while (num > 0) {


if (num % 10 >= lastDigit)
return 0; // Not a growing number
lastDigit = num % 10;
num /= 10;
}
return 1; // It's a growing number
}

void verifyNumber(int num, int process_id) {


if (isGrowingNumber(num)) {
printf("%d is a growing number on process %d\n", num,
process_id);
} else {
printf("%d is not a growing number on process %d\n", num,
process_id);
}
}

int main(int argc, char *argv[])


{ if (argc < 2) {
printf("Usage: %s <num1> <num2> ... <numN>\n", argv[0]);
return 1;
}

for (int i = argc-1; i >= 1; i--)


{ int num = atoi(argv[i]);
if (num <= 0) {
printf("%s is not a positive integral number\n",
argv[i]);
continue;
}

pid_t pid = fork();

if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
verifyNumber(num, getpid());
exit(EXIT_SUCCESS);
}
}

// Parent process waits for all child processes to finish


int status;
while (wait(&status) > 0);

return 0;
}
Screenshot:

Problem 2.2: Multi-process Prime program

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>

bool isPrime(int num)


{ if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++)
{ if (num % i == 0) {
return false;
}
}
return true;
}

void verifyPrime(int num, int process_id)


{ if (isPrime(num)) {
printf("%d is a prime on process %d\n", num, process_id);
} else {
printf("%d is not a prime on process %d\n", num,
process_id);
}
}

int main(int argc, char *argv[])


{ if (argc < 2) {
printf("Usage: %s <num1> <num2> ... <numN>\n", argv[0]);
return 1;
}

for (int i = argc-1; i >= 1; i--)


{ int num = atoi(argv[i]);
if (num <= 0) {
printf("%s is not a positive integral number\n",
argv[i]);
continue;
}

pid_t pid = fork();

if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
verifyPrime(num, getpid());
exit(EXIT_SUCCESS);
}
}

// Parent process waits for all child processes to finish


int status;
while (wait(&status) > 0);

return 0;
}

Screenshot:
Problem 2.2: Multi-process Fibonacci program

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <math.h>
#include <sys/types.h>
#include <sys/wait.h>

bool isPerfectSquare(int num) {


int squareRoot = sqrt(num);
return squareRoot * squareRoot == num;
}

bool isFibonacci(int num) {


return isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 *
num * num - 4);
}

void verifyFibonacci(int num, int process_id)


{ if (isFibonacci(num)) {
printf("%d is a Fibonacci member on process %d\n", num,
process_id);
} else {
printf("%d is not a Fibonacci member on process %d\n", num,
process_id);
}
}

int main(int argc, char *argv[])


{ if (argc < 2) {
printf("Usage: %s <num1> <num2> ... <numN>\n", argv[0]);
return 1;
}

for (int i = argc-1; i >= 1; i--)


{ int num = atoi(argv[i]);
if (num <= 0) {
printf("%s is not a positive integral number\n",
argv[i]);
continue;
}

pid_t pid = fork();

if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
verifyFibonacci(num, getpid());
exit(EXIT_SUCCESS);
}
}

// Parent process waits for all child processes to finish


int status;
while (wait(&status) > 0);

return 0;
}

Screenshot:

You might also like