Professional Documents
Culture Documents
memory.
PROGRAM DESCRIPTION:
Shared Memory - is an efficeint means of passing data between programs. One program will
create a memory portion which other processes (if permitted) can access.A process creates a
shared memory segment using shmget(),The original owner of a shared memory segment can
assign ownership to another user with shmctl(). It can also revoke this assignment. Other
processes with proper permission can perform various control functions on the shared memory
segment using shmctl(). Once created, a shared segment can be attached to a process
address space using shmat(). It can be detached using shmdt() The attaching process must have
the appropriate permissions for shmat(). Once attached, the process can read or write to the
segment, as allowed by the permission requested in the attach operation. A shared segment can
be attached multiple times by the same process. A shared memory segment is described by a
control structure with a unique ID that points to an area of physical memory. The identifier of
the segment is called the shmid. The structure definition for the shared memory segment
control structures and prototype can be found in <sys/shm.h>
One shmid data structure for each shared memory segment in the system. */
struct shmid_ds {
struct ipc_perm shm_perm; /* operation perms */
int shm_segsz; /* size of segment (bytes) */ time_t
shm_atime; /* last attach time */
time_t shm_dtime; /* last detach time */ time_t
shm_ctime; /* last change time */ unsigned short
shm_cpid; /* pid of creator */ unsigned short
shm_lpid; /* pid of last operator */ short
shm_nattch; /* no. of current attaches */ ` }
PROGRAME CODE :
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<sys/ipc.h>
static int pid,semid,shmid,cntid;
char *ptr,*ctr;
struct sembuf sop;
main()
{
int i;
semid=semget((key_t)10,1,IPC_CREAT|0666);
shmid=shmget((key_t)11,100,IPC_CREAT|0666);
cntid=shmget((key_t)12,2,IPC_CREAT|0666);
semctl(semid,0,SETVAL,1);
ptr=(char*)shmat(shmid,0,0);
ctr=(char*)shmat(cntid,0,0); ctr[i];
pid=fork();
if(pid==0)
{
printf("producer starts \n");
producer();
{
var=ptr[i];
j--;
printf("\n consumer %d",var);
code2();
semop(semid,&sop,1);
sleep(4);
}
}
}
OUTPUT:
[sudha@linuxserver semaphores]$ ./a.out
producer starts
consumer starts
producing 0
consuming 0
producing 1
consuming 1
producing 2
consuming 2
producing 3
consuming 3
producing 4
producer ends
consuming 4
consumer ends
EXPERIMENT 11: Readers – Writers problem using message passing.
PROGRAME CODE :
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <pthread.h>
#define SIZE 1024
#define SHMKEY1 (key_t)24
#define SHMKEY2 (key_t)25
#define SEMKEY1 (key_t)26
#define SEMKEY2 (key_t)27
struct databuf
{
int n;
char buf[SIZE];
};
static int wrt, shmidl, readcount, mutex;
struct sembuf sp1,sp2,sv1,sv2;
struct databuf *buff;
char *count;
void writer()
{
semop(wrt,&sp1,1);
printf("\n In the writer process, pid is : %d\n", getpid());
strcpy(buff->buf,"Hello this is writer"); /*strcat(buff-
>buf,(char*)getpid());*/ printf("\n Writer ended");
semop(wrt,&sv1,1);
}
void reader()
{
char str[100];
semop(mutex,&sp2,1);
printf("\n In reader process, pid is : %d\n", getpid());
count[0]+=1;
if(count[0]==1)
{
semop(wrt,&sp1,1);
}
semop(mutex,&sv2,1);
strcpy(str,buff->buf);
printf("\n no. of readers in critical section are %d ", count[0]);
printf("\n read message is %s, my id =%d",str, getpid());
semop(mutex,&sp2,1);
count[0]-=1;
printf("\n no. of readers in critical section are %d", count[0]);
if(count[0]==0)
{
semop(wrt,&sv1,1);
}
semop(mutex,&sv2,1);
}
int i = 0;
void * threadfunctionread(void* param)
{
printf("\nChild Thread with id %u started\n",pthread_self());
++i;
printf("Now threads are: %d\n", i);
reader();
printf("\nChild Thread with id %u ended\n\n",pthread_self());
pthread_exit(0);
}
void * threadfunctionwrite(void* param)
{
printf("\nChild Thread with id %u started\n",pthread_self());
++i;
printf("Now threads are: %d\n", i);
writer();
printf("\nChild Thread with id %u ended\n\n",pthread_self());
pthread_exit(0);
}
main()
{
int p1,p2;
wrt=semget(SEMKEY1,1,IPC_CREAT|0666);
mutex=semget(SEMKEY2,1,IPC_CREAT|0666);
semctl(wrt,0,SETVAL,1);
semctl(mutex,0,SETVAL,1);
shmidl=shmget(SHMKEY1,sizeof(struct databuf),IPC_CREAT|0666);
readcount=shmget(SHMKEY2,2,IPC_CREAT|0666);
count=(char *)shmat(readcount,0,0);
count[0]=0;
buff=(struct databuf*)shmat(shmidl,0,0);
sp1.sem_num=0;
sp1.sem_op=-1;
sp1.sem_flg=0;
sv1.sem_num=0;
sv1.sem_op=1;
sv1.sem_flg=0;
sp2.sem_num=0;
sp2.sem_op=-1;
sp2.sem_flg=0;
sv2.sem_num=0;
sv2.sem_op=1;
sv2.sem_flg=0;
pthread_t t1, t2, t3, t4, t5;
printf("\nMain thread id is %u\n\n",pthread_self());
pthread_create(&t1, NULL, threadfunctionread, NULL);
pthread_create(&t2, NULL, threadfunctionwrite, NULL);
pthread_create(&t3, NULL, threadfunctionread, NULL);
pthread_create(&t4, NULL, threadfunctionread, NULL);
pthread_create(&t5, NULL, threadfunctionread, NULL);
pthread_join(t1, 0);
pthread_join(t2, 0);
pthread_join(t3, 0);
pthread_join(t4, 0);
pthread_join(t5, 0);
semctl( wrt, 0, IPC_RMID, NULL );
semctl( mutex, 0, IPC_RMID, NULL );
shmctl( shmidl, IPC_RMID, NULL );
shmctl( readcount, IPC_RMID, NULL );
printf("\nMain Thread with id %u ended\n",pthread_self());
}
OUTPUT:
[sudha@linuxserver ~]$ cc rewriters.c -pthread
[sudha@linuxserver ~]$ ./a.out
Main thread id is 3086821056
Child Thread with id 3086818192 started
Now threads are: 1
In reader process, pid is : 12307
Child Thread with id 3044858768 started
Now threads are: 2
In reader process, pid is : 12307
no. of readers in critical section are 2
read message is , my id =12307
no. of readers in critical section are 1
Child Thread with id 3044858768 ended
Child Thread with id 3055348624 started
Now threads are: 3
In reader process, pid is : 12307
no. of readers in critical section are 2
read message is , my id =12307
no. of readers in critical section are 1
Child Thread with id 3055348624 ended
Child Thread with id 3076328336 started
Now threads are: 4
Child Thread with id 3065838480 started
Now threads are: 5
In reader process, pid is : 12307
no. of readers in critical section are 2
read message is , my id =12307
no. of readers in critical section are 1
Child Thread with id 3065838480 ended
no. of readers in critical section are 1
read message is , my id =12307
no. of readers in critical section are 0
Child Thread with id 3086818192 ended
In the writer process, pid is : 12307
Writer ended
Child Thread with id 3076328336 ended
Main Thread with id 3086821056 ended
EXPERIMENT 12: Dinning philosopher problem using semaphore
PROGRAM DESCRIPTION:
The program implements Dining philosopher problem which is a circular permutation based
synchronization problem using Semaphores and shared memory.The dining-philosophers
problem is considered a classic synchronization because it is an example of a large class of
concurrency-control problems. It is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner. Consider
five
philosophers who spend their lives thinking and eating. The philosophers share a circular table
surrounded by five chairs, each belonging to one philosopher. In the center of the table is a
bowl of rice, and the table is laid with five single chopsticks .When a philosopher thinks, she
does not interact with her colleagues. From time to time, a philosopher gets hungry and tries to
pick up the two chopsticks that are closest to her (the chopsticks that are between her and her
left and right neighbors). A philosopher may pick up only one chopstick at a time. Obviously,
she cannot pick up a chopstick that is already in the hand of a neighbor. When a hungry
philosopher has both her chopsticks at the same time, she eats without releasing her chopsticks.
When she is finished eating, she puts down both of her chopsticks and starts thinking again.
One simple solution is to represent each chopstick with a semaphore. A philosopher tries to
grab a chopstick by executing a wait () operation on that semaphore; she releases her
chopsticks by executing the signal() operation on the appropriate semaphores. Thus, the shared
data are semaphore chopstick[5]; where all the elements of chopstick are initialized to 1. Use
an asymmetric solution; that is, an odd philosopher picks up first her left chopstick and then
her right chopstick,
whereas an even philosopher picks up her right chopstick and then her left chopstick
ALGORITHM :
PROGRAME CODE :
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
static int chopstick;
struct sembuf sop;
int main()
{
int p1,p2,p3;
chopstick=semget((key_t)0X25,5,IPC_CREAT|0666);
semctl(chopstick,0,SETVAL,1);
semctl(chopstick,1,SETVAL,1);
semctl(chopstick,2,SETVAL,1);
semctl(chopstick,3,SETVAL,1);
semctl(chopstick,4,SETVAL,1); p1=fork();
p2=fork();
// printf("%d%d",p1,p2);
if(p1==0)
{
if(p2==0)
{
p3=fork();
if(p3==0)
{
sleep(2);
philosopher(2);
}
else
{
sleep(3);
philosopher(3);
}
}
else
{
sleep(1);
philosopher(4);
}
}
else
{
if(p2==0)
{
sleep(2);
philosopher(1);
}
else
{
sleep(4);
philosopher(0);
}
}}
philosopher(int i)
{
while(1)
{
if(i==0||i==2||i==4)
{
printf("\n philosopher %d is thinking",i);
sleep(5);
wait_b(chopstick,i);
wait_b(chopstick,((i+1)%5));
printf("\n philosopher %d has acquired chopsticks",i);
sleep(5);
printf("\n philosopher %d is eating",i);
sleep(5);
signal_b(chopstick,i);
signal_b(chopstick,((i+1)%5));
printf("\n philosopher %d has released or returned the chopstick",i);
sleep(5);
}
else
{
printf("\n philosopher %d is thinking",i);
sleep(5);
wait_b(chopstick,((i+1)%5));
wait_b(chopstick,i);
printf("\nphilosopher %d has acquired both chopstick",i);
sleep(5);
printf("\nphilospher %d is eating");
sleep(5);
signal_b(chopstick,((i+1)%5));
signal_b(chopstick,i);
printf("\n philosopher %d has released chopsticks",i);
sleep(5);
}
}
}
wait_b(int semid,int semnum)
{
sop.sem_num=semnum;
sop.sem_op=-1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
OUTPUT:
[sudha@linuxserver ~]$ ./a.out
philosopher 4 is thinking
philosopher 2 is thinking
philosopher 2 has acquired chopsticks
philosopher 2 is eating
philosopher 2 has released or returned the chopstick
philosopher 1 is thinking
philosopher 1 has acquired both chopstick
philospher 1 is eating
AIM: To write a program to implement Bankers algorithm for Deadlock detection and
avoidance
PROGRAM DESCRIPTION:
ALGORITHM:
Step 2:Obtain the required data through char and int datatypes.
Step 3:Enter the filename,index block.
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
intmain()
count = 0;
scanf("%d", &p);
completed[i] = 0;
scanf("%d", &r);
scanf("%d", &alloc[i][j]);
scanf("%d", &avail[i]);
do
printf("\t\t");
process = -1;
process = i ;
process = -1;
break; } } }
if(process != -1)
break;
if(process != -1)
safeSequence[count] = process + 1;
count++;
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
} } }
if(count == p)
printf(">\n");
} else
OUTPUT
shell scripts.
AIM: To write a program to implement LINUX shell scripts.
15.1. Write a shell program to print the host name and userdefined message.
15.5. Write a shell program to find whether a given number is even or odd.
PROGRAM DESCRIPTION:
The shell is the outermost layer of the operating system. Shells incorporate a programming
language to control processes and files, as well as to start and control other programs. The shell
manages the interaction between you and the operating system by prompting you for input,
interpreting that input for the operating system, and then handling any resulting output from
the operating system.
Shells provide a way for you to communicate with the operating system. This communication
is carried out either interactively (input from the keyboard is acted upon immediately) or as a
shell script. A shell script is a sequence of shell and operating system commands that is stored
in a file.
When you log in to the system, the system locates the name of a shell program to execute.
After it is executed, the shell displays a command prompt. This prompt is usually a $ (dollar
sign). When you type a command at the prompt and press the Enter key, the shell evaluates the
command and attempts to carry it out. Depending on your command instructions, the shell
writes the command output to the screen or redirects the output. It then returns the command
prompt and waits for you to type another command.
A command line is the line on which you type. It contains the shell prompt. The basic format
for each line is as follows:
$ Command Argument(s)
The shell considers the first word of a command line (up to the first blank space) as the
command and all subsequent words as arguments.
Note: When libc.a is moved or renamed, the Killed error message is displayed from the shell
because there is no libc.a file available for the system to load and run the utilities. The recsh
command invokes the recovery shell, which provides an ability to rename libc.a if it is
accidently moved.
Shell concepts
Before you start working with the different types of shells available for AIX you need to
understand basic terminology and features.
Korn shell
The Korn shell (ksh command) is backwardly compatible with the Bourne shell (bsh
command) and contains most of the Bourne shell features as well as several of the best
features of the C shell.
Bourne shell
The Bourne shell is an interactive command interpreter and command programming
language.
C shell
The C shell is an interactive command interpreter and a command programming language. It
uses syntax that is similar to the C programming language.
A shell is a piece of software that provides an interface for users of an operating system which
provides access to the services of a kernel. However, the term is also applied very loosely to
applications and may include any software that is "built around" a particular component, such
as web browsers and email clients that are "shells" for HTML rendering engines. The name
shell originates from shells being an outer layer of interface between the user and the internals
of the operating system (the kernel).
PROGRAM CODE
SHELL PROGRAMMING :
1. Write a shell program to print the host name and userdefined message.
CODE :
read n
OUTPUT:
irfan
hi irfan
goodbye irf
CODE :
read n1
read n2
if [ "$n1" = "$n2" ]
then
echo "both are equal numbers"
else
fi
OUTPUT:
enter n1
enter n2
Run-2:
enter n1 2
enter n2 2
CODE :
read str
x= echo "$str"|wc -c
echo "$((x-1))"
OUTPUT:
irfan
-1
number. CODE :
read n
n1=$n
fact=1
while [ $n -ge 1 ]
do
fact=$(($n * $fact))
n=$(($n - 1))
done
echo "$fact"
OUTPUT:
[sudha@linuxserver~]$ ./fact.sh
enter a number 6
720
CODE :
read n1
n2=$(($n1%2))
if [ $n2 -eq 0 ]
then
else
fi
OUTPUT:
[sudha@linuxserver~]$ ./evod.sh
Run-1:
enter a number 4
number is even
Run-2:
[sudha@linuxserver~]$ ./evod.sh
enter a number 7
number is odd
CODE :
read n2
read n3
then
then
then
else
fi
OUTPUT:
[sudha@linuxserver~]$ ./gtno.sh
enter n1: 2
enter n2: 5
enter n3: 4
5 is greatest
7. Write a shell program to find file or directory name.
CODE :
read fn
if [ -f $fn ]
then
elif [ -d $fn ]
then
else
fi
OUTPUT:
[sudha@linuxserver~]$ ./file.sh
Viva QUESTIONS
Define file?
What are the different kinds of files?
What is the purpose of file allocation strategies?
Identify ideal scenarios where sequential, indexed and linked file allocation strategies
are most appropriate?
What are the disadvantages of sequential file allocation strategy?
What is an index block?
What is the file allocation strategy used in UNIX?
Which of the dynamic contiguous memory allocation strategies suffer with external
fragmentation?
What are the possible solutions for the problem of external fragmentation?
What is 50-percent rule?
What is compaction?
Which of the memory allocation techniques first-fit, best-fit, worst-fit is efficient?
Why?
What are the advantages of noncontiguous memory allocation schemes?
What is the process of mapping a logical address to physical address with respect to
the paging memory management technique?
Define the terms – base address, offset?
Define directory?
Describe the general directory structure?
List the different types of directory structures?
Which of the directory structures is efficient? Why?
Which directory structure does not provide user-level isolation and protection?
What is the advantage of hierarchical directory structure?