sarath
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
// Create a new process
pid = fork();
if (pid < 0) {
// Error occurred
perror("fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("This is the child process. PID: %d\n", getpid());
} else {
// Parent process
printf("This is the parent process. PID: %d, Child PID: %d\n", getpid(), pid);
}
return 0;
}
------------------------------------------------------------
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
3
Γ
#define N 5
L
sem_t forks[N];
pthread_t phil[N];
int id[N];
void* dine(void* arg) {
int i = (int)arg;
printf("Philosopher %d is thinking\n", i);
sleep(1);
sem_wait(&forks[i]);
sem_wait(&forks[(i + 1) % N]);
printf("Philosopher %d is eating\n", i);
sleep(2);
sem_post(&forks[i]);
sem_post(&forks[(i + 1) % N]);
printf("Philosopher %d finished eating\n", i);
return NULL;
}
int main() {
for (int i = 0; i < N; i++) {
sem_init(&forks[i], 0, 1);
id[i] = i; }
3
Γ
for (int i = 0; i < N; i++)
pthread_create(&phil[i], NULL, dine, &id[i]);
for (int i = 0; i < N; i++)
pthread_join(phil[i], NULL);
for (int i = 0; i < N; i++)
sem_destroy(&forks[i]);
return 0;
}
-----------------------------------------------------------------------------------------------
#include <unistd.hs
int maincy
{ char *args[]"["IS", "-1", NULL},
execup (args [o] args);
Teturn 1;
}
------------------------------
9b)#include <stdio.h>
3
#define SIZE 5
int main() {
int partitions[SIZE] = {100, 500, 200, 300, 600); // Partition sizes
int allocated[SIZE] = {0};
// Allocation flags
int process] = {212, 417, 112, 426};
// Process sizes
}
}
int i, j;
for (i = 0; i < 4; i++) {
int bestldx = -1;
for (j = 0; j < SIZE; j++) {
if (!allocated[j] && partitions[j] >= process[i]) {
if (bestldx == -1 || partitions[j] < partitions[bestIdx]) {
bestldx = j;
}
if (bestldx != -1) {
allocated[bestldx] = 1;
printf("Process %d allocated to partition %d\n", process[i],
bestldx + 1);
} else {
3
printf("Process %d not allocated\n", process[i]);
}
}
return 0;
}
Comments
Post a Comment