Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

pthreads (1)

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include <stdio.

h>

struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};

void fcfs_scheduling(struct Process processes[], int n) {


int total_waiting_time = 0, total_turnaround_time = 0;

printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");

for (int i = 0; i < n; i++) {


printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].pid, processes[i].arrival_time, processes[i].burst_time,
processes[i].waiting_time, processes[i].turnaround_time);
}

printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}

int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process processes[n];

printf("Enter arrival and burst time for the processes:\n");

for (int i = 0; i < n; i++) {


processes[i].pid = i + 1;

printf("Process %d Arrival Time: ", i + 1);


scanf("%d", &processes[i].arrival_time);

printf("Process %d Burst Time: ", i + 1);


scanf("%d", &processes[i].burst_time);
}

fcfs_scheduling(processes, n);
return 0;}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

#define BUFFER_SIZE 1024

void copy(char*, char*);

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


if (argc != 3) {
fprintf(stderr, "ERROR: usage: %s <src> <dst>\n", argv[0]);
exit(1);
}

printf("Copying %s to %s\n", argv[1], argv[2]);


copy(argv[1], argv[2]);

return 0;
}

void copy(char *src, char *dst) {


int sfd, dfd, count;
char buffer[BUFFER_SIZE];
mode_t fileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

sfd = open(src, O_RDONLY);


if (sfd < 0) {
fprintf(stderr, "ERROR: %s doesn't exist or cannot be opened for reading\n", src);
exit(1);
}

dfd = open(dst, O_CREAT | O_WRONLY | O_TRUNC, fileMode);


while ((count = read(sfd, buffer, BUFFER_SIZE)) > 0) {
write(dfd, buffer, count);
}

close(sfd);
close(dfd);
}
#include <stdio.h>
#include <stdlib.h>

int main() {
printf("\nKernel Version:\n");
system("cat /proc/version");
printf("\nInformation on Configured amount of free and used memory\n");
system("cat /proc/meminfo | awk '/Mem/ {print}'");

return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main() {
printf("Kernel Version:\n");
system("cat /proc/version");

printf("\nCPU Type and Model:\n");


system("cat /proc/cpuinfo | grep 'processor\\|model'");

return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>

void sameprogramsamecode(){
system("clear");
fprintf(stdout,"Now parent and child will execute same code print pid of themselves
and then exit\n");
fprintf(stdout,"pid: %d\n", getpid());
exit(1);
}

void sameprogramdifferentcode(){
fprintf(stdout,"Now we will execute different code in parent and child\n");
pid_t pid = fork();
if(pid == 0){
fprintf(stdout,"Inside child with pid: %d\n", getpid());
exit(1);
} else if(pid > 0){
fprintf(stdout,"Inside parent with pid: %d\n", getpid());
} else {
fprintf(stderr,"ERROR: while forking");
}
}

void sameprogramwaitforchildtoexit(){
fprintf(stdout,"Now we will execute different code in parent and child and parent
will wait for child\n");
pid_t pid = fork();
if(pid == 0){
fprintf(stdout,"Inside child with pid: %d\n", getpid());
fprintf(stdout,"Exiting child\n");
exit(1);
} else if(pid > 0){
sleep(1);
fprintf(stdout,"Inside parent with pid: %d\n", getpid());
fprintf(stdout,"Parent waiting for child\n");
wait(NULL);
fprintf(stdout,"Exiting parent\n");
} else {
fprintf(stderr,"ERROR: while forking");
exit(1);
}
}

int main(){
int choice;
while(1){
printf("1) Same program and same code\n");
printf("2) Same program and different code\n");
printf("3) Before terminating the parent wait for the child to finish its task\n");
printf("4) Enter 0 to exit\n");
scanf("%d", &choice);
switch(choice){
case 0:
exit(0);
case 1:
sameprogramsamecode();
break;
case 2:
sameprogramdifferentcode();
break;
case 3:
sameprogramwaitforchildtoexit();
break;
default:
printf("ERROR: wrong choice try again...\n");
}
}
return 0;
}

You might also like