OS Lab All Programs With Output - 241023 - 211514
OS Lab All Programs With Output - 241023 - 211514
List.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
if (argc != 2)
{
printf("Usage: list <directory>\n");
exit(0);
}
dir = opendir(argv[1]);
while(file = readdir(dir))
{
stat(file->d_name, &stats);
printf("\t%ld", stats.st_nlink);
tf = getpwuid(stats.st_uid);
printf("\t%s", tf->pw_name);
gf = getgrgid(stats.st_gid);
printf("\t%s", gf->gr_name);
closedir(dir);
}
Copy.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
if (argc != 3)
{
printf("Usage: copy <src_file> <dest_file>\n");
exit(0);
}
close(fd_src);
close(fd_dest);
}
Move.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
if (argc != 3)
{
printf("Usage: copy <src_file> <dest_file>\n");
exit(0);
}
close(fd_src);
close(fd_dest);
remove(argv[1]);
}
Remove.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: remove <file>\n");
exit(0);
}
remove(argv[1]);
}
Fork.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<wait.h>
int main()
{
pid_t pid = fork();
if(pid < 0)
{
printf("Fork failed\n");
exit(0);
}
else if(pid == 0)
{
printf("Child: pid value = %d\n", pid);
printf("Child: Process id = %d\n", getpid());
printf("Child: Parent-Process id = %d\n", getppid());
printf("Child: Exiting from child\n");
exit(0);
}
else
{
printf("Parent: Waiting for child\n");
wait(0);
printf("Parent: pid value = %d\n", pid);
printf("Parent: Process id = %d\n", getpid());
printf("Parent: Child-Process id = %d\n", pid);
}
return 0;
}
Execl.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<wait.h>
if(pid < 0)
{
printf("Fork failed\n");
exit(0);
}
else if(pid == 0)
{
printf("Child:\n");
execl("sum_diff", "sum_diff", argv[1], argv[2], NULL);
exit(0);
}
else
{
wait(0);
printf("Parent:\n");
}
}
Sum_diff.c
#include<stdio.h>
#include<stdlib.h>
Thread.c
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int sum = 0;
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, func, argv[1]);
pthread_join(tid, NULL);
Producer_consumer.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#define MAX 5
sem_t in_sem;
sem_t rm_sem;
roducer()
void* p
{
int count = 0;
while(1)
{
sem_wait(&in_sem);
sleep(rand()%3);
printf("Producer inserted at %d\n", count % MAX + 1);
count++;
sem_post(&rm_sem);
}
}
onsumer()
void* c
{
int count = 0;
while(1)
{
sem_wait(&rm_sem);
printf("Consumer removed at %d\n", count % MAX + 1);
sleep(rand()%3);
count++;
sem_post(&in_sem);
}
}
int main()
{
pthread_t threads[2];
sem_init(&in_sem, 0, MAX);
sem_init(&rm_sem, 0, 0);
srand(time(NULL));
ULL);
pthread_join(threads[0], N
pthread_join(threads[1], N ULL);
sem_destroy(&in_sem);
sem_destroy(&rm_sem);
}
Reader_writer.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#define N 10
int process[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int r_count = 0;
sem_t rw_sem;
sem_t rc_sem;
eader(void* num)
void* r
{
int n = *(int*)num;
sem_wait(&rc_sem);
if(!r_count)
{
printf("Waiting for reading\n");
sem_wait(&rw_sem);
}
r_count++;
sem_post(&rc_sem);
riter(void* num)
void* w
{
int n = *(int*)num;
printf("Finished writing\n");
sem_post(&rw_sem);
}
int main()
{
pthread_t threads[N];
sem_init(&rw_sem, 0, 1);
sem_init(&rc_sem, 0, 1);
srand(time(NULL));
int r = rand()%N;
sem_destroy(&rw_sem);
sem_destroy(&rc_sem);
}
Dining_philosopher.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT(i) (((i)+4)%N)
#define RIGHT(i) (((i)+1)%N)
int phil[N] = { 0, 1, 2, 3, 4 };
int p_state[N] = {};
sem_t eat_sem[N];
sem_t st_sem;
void test(int n)
{
if(p_state[n] == HUNGRY && p_state[LEFT(n)] != EATING &&
p_state[RIGHT(n)] != EATING)
{
p_state[n] = EATING;
printf("Philosopher %d is taking chopsticks %d and %d\n", n+1,
n+1, LEFT(n)+1);
printf("Philosopher %d is eating\n", n+1);
sem_post(&eat_sem[n]);
}
}
void take_fork(int n)
{
sem_wait(&st_sem);
p_state[n] = HUNGRY;
printf("Philosopher %d is hungry\n", n+1);
test(n);
sem_post(&st_sem);
}
void keep_fork(int n)
{
sem_wait(&st_sem);
p_state[n] = THINKING;
printf("Philosopher %d is keeping chopsticks %d and %d\n", n+1, n+1,
LEFT(n)+1);
printf("Philosopher %d is thinking\n", n+1);
test(LEFT(n));
test(RIGHT(n));
sem_post(&st_sem);
}
int main() {
pthread_t threads[N];
sem_init(&st_sem, 0, 1);
for(int i = 0; i < N; i++)
sem_init(&eat_sem[i], 0, 0);
sem_destroy(&st_sem);
for(int i = 0; i < N; i++)
sem_destroy(&eat_sem[i]);
}
Lock.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
if(argc != 3)
{
printf("Usage: lock <file> <lock_type (RD:0, WR:!0)>\n");
exit(0);
}
fd = open(argv[1], O_RDWR);
if(fd == -1)
{
printf("Error while opening the file\n");
exit(1);
}
lock.l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, &lock) == -1)
{
printf("Unable unlock the file\n");
exit(0);
}
printf("File unlocked\n");
}
close(fd);
}
File unlocked
sourav@ubuntu-VirtualBox:~/Documents/OS/os6$ ./a.out lock.txt 1
File write locked
Press enter to release lock
;<2>
File unlocked
;<1>
sourav@ubuntu-VirtualBox:~/Documents/OS/os6$ ./a.out lock.txt 0
File read locked
Press enter to release lock
File unlocked
sourav@ubuntu-VirtualBox:~/Documents/OS/os6$ ./a.out lock.txt 1
Unable lock the file
File already locked by process (pid):6149
;<2>
sourav@ubuntu-VirtualBox:~/Documents/OS/os6$ ./a.out lock.txt 0
Unable lock the file
File already locked by process (pid):6188
sourav@ubuntu-VirtualBox:~/Documents/OS/os6$ ./a.out lock.txt 1
Unable lock the file
File already locked by process (pid):6188
7. Write a program to detect deadlocks using Banker’s algorithm in C.
Banker.c
#include<stdio.h>
int main()
{
int processes, resources;
int i, j;
int max[processes][resources];
int allocated[processes][resources];
int need[processes][resources];
int total[resources];
int available[resources];
int completed[processes];
printf("Execution Sequence:\n");
int finished = 0;
int last_fin = 0;
i = 0;
do
{
if(completed[i])
{
i = (i+1) % processes;
continue;
}
int satisfied = 0;
for(j = 0; j < resources; j++)
{
if(need[i][j] <= available[j])
satisfied++;
}
if(satisfied == resources)
{
completed[i] = 1;
for(j = 0; j < resources; j++)
available[j] += allocated[i][j];
printf("P[%d] ", i+1);
finished++;
last_fin = i;
}
if(finished == processes)
break;
i = (i+1) % processes;
} while(i != last_fin);
if(finished == processes)
printf("\nSafe state : No deadlock\n");
else
printf("\nNot safe state : Deadlock\n");
}
Buddy.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct {
char size;
char* name;
} tree[32] = {};
int max;
int level = 0;
int s = max;
while(1)
{
if(req < s && req > s/2)
break;
s /= 2;
level++;
}
if(node == last)
{
printf("Process %s is not present in memory\n", name);
return;
}
tree[node].size = 0;
free(tree[node].name);
tree[node].name = NULL;
printf("Successful deallocation\n");
while(node != 1)
{
int sib = node%2 == 0 ? node + 1 : node - 1;
if(tree[sib].size == 0 && tree[node].size == 0)
{
node /= 2;
tree[node].size = 0;
}
else break;
}
}
else
printf("%d: %s - %d\n", size, tree[node].name,
tree[node].size);
}
int main()
{
int c, req;
char name[10];
printf("B U D D Y S Y S T E M R E Q U I R E M E N T S\n");
switch(c)
{
case 1:
printf("\nM E M O R Y A L L O C A T I O N\n");
printf("Enter the process name:\n");
scanf("%9s", name);
printf("Enter the process size:\n");
scanf("%d", &req);
mem_alloc(name, req);
break;
case 2:
printf("\nM E M O R Y D E A L L O C A T I O
N\n");
printf("Enter the process name:\n");
scanf("%9s", name);
mem_dealloc(name);
break;
case 3:
printf("\nM E M O R Y A L L O C A T I O N M A
P\n");
mem_print(1, 0);
break;
default:
return 0;
}
}
}
sourav@ubuntu-VirtualBox:~/Documents/OS/os8$ gcc buddy.c
sourav@ubuntu-VirtualBox:~/Documents/OS/os8$ ./a.out
B U D D Y S Y S T E M R E Q U I R E M E N T S
Enter the total size of the memory:
1024
B U D D Y S Y S T E M
1. Locate the process into memory
2. Remove the process from memory
3. Tree structure for memory allocation map
<other> Exit
Enter your choice:
1
M E M O R Y A L L O C A T I O N
Enter the process name:
P1
Enter the process size:
99
Successful allocation
B U D D Y S Y S T E M
1. Locate the process into memory
2. Remove the process from memory
3. Tree structure for memory allocation map
<other> Exit
Enter your choice:
1
M E M O R Y A L L O C A T I O N
Enter the process name:
P2
Enter the process size:
279
Successful allocation
B U D D Y S Y S T E M
1. Locate the process into memory
2. Remove the process from memory
3. Tree structure for memory allocation map
<other> Exit
Enter your choice:
3
M E M O R Y A L L O C A T I O N M A P
1024 ---> DIVIDED
| 512 ---> DIVIDED
| | 256 ---> DIVIDED
| | | 128: P1 - 99
| | | 128 ---> FREE
| | 256 ---> FREE
| 512: P2 - 23
B U D D Y S Y S T E M
1. Locate the process into memory
2. Remove the process from memory
3. Tree structure for memory allocation map
<other> Exit
Enter your choice:
2
M E M O R Y D E A L L O C A T I O N
Enter the process name:
P1
Successful deallocation
B U D D Y S Y S T E M
1. Locate the process into memory
2. Remove the process from memory
3. Tree structure for memory allocation map
<other> Exit
Enter your choice:
3
M E M O R Y A L L O C A T I O N M A P
1024 ---> DIVIDED
| 512 ---> FREE
| 512: P2 - 23
B U D D Y S Y S T E M
1. Locate the process into memory
2. Remove the process from memory
3. Tree structure for memory allocation map
<other> Exit
Enter your choice:
0
9. Demonstrate the working of static and dynamic linking of libraries
in C.
Prog.c
#include<stdio.h>
#include<stdlib.h>
#include"sum.c"
#include"diff.c"
int a = atoi(argv[1]);
int b = atoi(argv[2]);
Sum.c
Diff.c