Wednesday, November 6, 2019

A7 B.2


7(B.2)
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
 
#define FIFO_NAME1 "comm_pipe1"
#define FIFO_NAME2 "comm_pipe2"
 
int main()
{
char s1[300],s2[300];
int num,fd1,fd2,byt;
//printf("producer");
mknod(FIFO_NAME1,S_IFIFO | 0666,0);
mknod(FIFO_NAME2,S_IFIFO | 0666,0);
 
printf("waiting for consumer..\n");
fd1=open(FIFO_NAME1, O_WRONLY);
fd2=open(FIFO_NAME2, O_RDONLY);
 
printf("got a consumer --type some stuff\n");
fgets( s1,20, stdin );
//gets(s1);
//printf("enter");
//scanf("%s",s1);
if((num=write(fd1,s1,strlen(s1)))==-1)
perror("write");
else
{
printf("Speak:wrote %d bytes to file 1\n",num);
byt=read(fd2,s2,300);
s2[byt]='\0';
printf("%s\n",s2);
}
return 0;
}
 
/**************************OUTPUT*************************
spllab01@spllab01-vostro:~$ gcc fast.c 
spllab01@spllab01-vostro:~$ ./a.out
waiting for consumer..
got a consumer --type some stuff
 iam shruti.    
Speak:wrote 13 bytes to file 1
for the given sentence the word count is 3
 vowel cnt is 5
 character count is 11
 linear are 0
spllab01@spllab01-vostro:~$ 
*/


A7 B.1


AIM :FIFOs: Full duplex communication between two independent processes. First process accepts sentences and writes on one pipe to be read by second process and second process counts number of characters, number of words and number of lines in accepted sentences, writes this output in a text file and writes the contents of the file on second pipe to be read by first process and displays on standard output.*
 
7(B.1)
 
 
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
 
#define FIFO_NAME1 "comm_pipe1"
#define FIFO_NAME2 "comm_pipe2"
 
int main()
{
char s[300],vowel[20],send[100];
int num,fd1,fd2,sig,k=0,i,wordcnt=1,charcnt=0,linecnt=0;
FILE *fp;
fp=fopen("fifo.text","w");
mknod(FIFO_NAME1,S_IFIFO | 0666,0);
mknod(FIFO_NAME2,S_IFIFO | 0666,0);
 
printf("waiting for poducers...\n");
fd1=open(FIFO_NAME1, O_RDONLY);
fd2=open(FIFO_NAME2, O_WRONLY);
printf("GOT A PRODUCER\n");
 
if((num=read(fd1,s,300))==-1)
perror("read");
else{
s[num]='\0';
printf("tick:read %d bytes :\%s\"\n",num,s);
k=0;
vowel[0]='\0';
wordcnt=1;
for(i=0;i<num;i++)
{
if((s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'))
{
vowel[k]=s[i];
k++;
}
if(s[i]==' '&&s[i+1]!=' ')
{
wordcnt++;
}
if(s[i]==' '&&(s[i+1]==' '||s[i+1]=='\0'))
linecnt++;
else
if(s[i]!=' '&&s[i]!=' ')
charcnt++;
}
vowel[k]='\0';
sprintf(send,"for the given sentence the word count is %d\n vowel cnt is %d\n character count is %d\n linear are %d\n",wordcnt,k,charcnt,linecnt);
fprintf(fp,"%s",send);
//strcat(snd,vowel);
if((sig=write(fd2,send,strlen(send)))!=-1)
printf("\nwritten successfully to file 2");
else
printf("\nerror in writing to file 2");
}
return 0;
}
 
 
 
 
/******************************************OUTPUT****************************************
 
/*spllab01@spllab01-vostro:~$ gcc furious.c 
spllab01@spllab01-vostro:~$ ./a.out
waiting for poducers...
GOT A PRODUCER
tick:read 13 bytes : iam shruti.
"
 
written successfully to file 2spllab01@spllab01-vostro:~$ 
*/


A2 b

B.
Implement the C program in which main program accepts an integer array. Main program uses the
FORK system call to create a new process called a child process. Parent process sorts an integer array
and passes the sorted array to child process through the command line arguments of EXECVE system
call. The child process uses EXECVE system call to load new program that uses this sorted array for
performing the binary search to search the particular item in the array.
Main program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int val[10],ele;
int n,i,j;
char *cval[10];
pid_t pid;
char *newe[]={NULL};
printf("Enter size of Array");
scanf("%d",&n);
printf("\nEnter Array");
for(i=0;i<n;i++)
{
scanf("%d",&val[i]);
}
printf("\nEntered Array is");
for(i=0;i<n;i++)
{
printf("\t%d",val[i]);
}
for(i=1;i<n;i++)
{ for(j=0;j<n-1;j++) {
if(val[j]>val[j+1])
{
int temp=val[j];
val[j]=val[j+1];
val[j+1]=temp;
} } }
printf("\nSorted Array is");
for(i=0;i<n;i++)
{
printf("\t%d",val[i]);
}
printf("\nEnter Element to search");
scanf("%d",&ele);
val[i]=ele;
for(i=0;i<n+1;i++)
{
char a[sizeof(int)];
snprintf(a,sizeof(int),"%d",val[i]);
cval[i]=malloc(sizeof(a));
strcpy(cval[i],a);

ASSIGNMENT NO 02

}
cval[i]=NULL;
pid=fork();
if(pid==0)
{
execve("./p2b",cval,newe);
perror("error");
}
}
Child program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char *argv[],char *en[])
{
int i,j,mid,ele;
int arr[argc];
for(j=0;j<argc-1;j++)
{
int n=atoi(argv[j]);
arr[j]=n;
}
ele=atoi(argv[j]);
i = 0;
j= argc - 1;
mid = (i+j)/2;
while (arr[mid]!=ele && i<=j)
{
if (ele>arr[mid])
i = mid + 1;
else
j=mid-1;
mid = (i+j)/2;
}
if(i<=j)
printf("Element is present in the list");
else
printf("not found!" );
}
/ *******************************************OUTPUT**************************************************
spllab01@spllab01-vostro:~$ gcc p2b.c -o p2b
spllab01@spllab01-vostro:~$ ./p2b
Element is present in the listspllab01@spllab01-vostro:~$ gcc p2a.c -o p2a
spllab01@spllab01-vostro:~$ ./p2a
Enter size of Array4
Enter Array2
1
6
4
Entered Array is 2 1 6 4
Sorted Array is 1 2 4 6
Enter Element to search4
spllab01@spllab01-vostro:~$ Element is present in the list*/

A7 a

AIM:- Pipes: Full duplex communication between parent and child processes. Parent process writes a pathname of a file (the contents of the file are desired) on one pipe to be read by child process
and child process writes the contents of the file on second pipe to be read by parent process and
displays on standard output.  
7(a).

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


int main(void)
{
int fd1[2],nbytes=1,fd2[2],a=0;
pid_t pid;
char string[80];
char readbuffer[80];
char ch='a',ch1='\n';
FILE *fp;
pipe(fd1);
pipe(fd2);

if((pid=fork())==-1)
{
perror("fork");
exit(1);
}

if(pid==0)
{
close(fd1[1]);
read(fd1[0],readbuffer,sizeof(readbuffer));

printf("filename %s is being read by child process through pipe 1\n\t",readbuffer);
fp=fopen(readbuffer,"r");
//close(fd1[0]);
close(fd2[0]);

printf("\n content of %s are being sent to parent process through pipe2\n\t",readbuffer);
while(a!=-1)
{
a=fscanf(fp,"%c",&ch);
write(fd2[1],&ch,sizeof(ch));
}
close(fd2[1]);
exit(0);
}
else
{
close(fd1[0]);
printf("\n\t In parent process\n");
printf("\n\tEnter name of file::\n\t");
scanf("%s",string);
printf("\n filenames %s is being set by parent process to child process through pipe1\n\t ",string);
write(fd1[1],string,(strlen(string)+1));
wait(NULL);
close(fd1[1]);
close(fd2[1]);
printf("\n content of %s are being received  by parent process through pipe2\n\t ",string);

printf("\n\tIn parent process");
printf("\n\tReceived message");
while(nbytes!=0)
{
printf("%c",ch1);
nbytes=read(fd2[0],&ch1,sizeof(ch1));
}
close(fd2[0]);
}
return(0);
}



*********************************************************OUTPUT****************************************************


/*spllab01@spllab01-vostro:~$ gcc admire.c
spllab01@spllab01-vostro:~$ ./a.out

                    In parent process

                   Enter name of file::
                   old.txt

 filenames old.txt is being set by parent process to child process through pipe1
filename old.txt is being read by child process through pipe 1
                  
 content of old.txt are being sent to parent process through pipe2
                                       
 content of old.txt are being received  by parent process through pipe2
                    
                   In parent process
                   Received message
*/

A9


Aim         :     Implement an assignment using File Handling System Calls (Low level system calls like open, read, write, etc)

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd, ch;
char fname[25];
char buffer[80];
ssize_t ret_in, ret_out;
char message[100];
do
{
printf("1.Create File\n");
printf("2.Write\n");
printf("3.Read\n");
printf("4.Exit\n");
printf("Enter your choice\t");
scanf("%d", &ch);
if(ch==1)
{
printf("Enter File Name\t");
scanf("%s",fname);
fd=creat(fname,S_IREAD| S_IWRITE);
if(fd==-1)
printf("Error in Opening file %s\n",fname);
else
{
printf("File %s is opened read/write access\n",fname);
printf("%s is currently empty\n",fname);
}
close(fd);
}
if(ch==2)
{
printf("Enter file name\t");
scanf("%s",fname);
fd=open(fname,O_WRONLY,1);
if(fd!=-1)
{
printf("file %s opened for write access\n",fname);
printf("Enter Data\t");
scanf("%s",message);
write(fd,message,sizeof(message));
close(fd);
}}
if(ch==3)
{
printf ("Enter file name\t");
scanf("%s",fname);
fd=open(fname,O_RDONLY,0);
if(fd!=-1)
{
printf("%s opened for read access\n",fname);
if(read(fd,buffer,sizeof(buffer)))
printf("\"%s\" was written to %s\n",buffer,fname);
else
printf("***Error reading %s **\n",fname);
}
close(fd);
}
}
while(ch!=4);
close(fd);
exit(0);
}
/*

A8


AIM   :     Inter-process Communication using Shared Memory using System .Application to demonstrate: Client and Server Programs in which server process creates a shared memory segment and writes the message to the shared memory segment. Client process reads the message from the shared memory segment and displays it to the screen

p8.c(CLIENT)

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdlib.h>
#define SHMSZ 27
int main()
{
int shmid;
key_t key;
char *shm,*s;
key=5678;
if((shmid=shmget(key,SHMSZ,0666))<0)
{
perror("Shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char *)-1)
{
perror("shmat");
exit(1);
}
for(s=shm;*s!=NULL;s++)
putchar(*s);
putchar('\n');
*shm='*';

exit(0);
}

p8b.c (server)

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<unistd.h>
#define SHMSZ 27
int main()
{
char c;
int shmid;
key_t key;
char *shm,*s;
key=5678;
if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
{
perror("Shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char *)-1)
{
perror("shmat");
exit(1);
}
s=shm;
for(c='a';c<='z';c++)
*s++=c;
*s=NULL;
while(*shm!='*')
sleep(1);
exit(0);
}

/*

A6


Aim:     Deadlock Avoidance Using Semaphores: Implement the deadlock-free solution to Dining Philosophers problem to illustrate the problem of deadlock and/or starvation that can occur when many synchronized threads are competing for limited resources.
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<unistd.h>
#define N 5
#define               THINKING 0
#define               HUNGRY 1
#define               EATING 2          
#define               Left (ph_num+4)%N
#define               Right (ph_num+1)%N
sem_t room;
sem_t chopst[N];
void *philospher(void *num);
void take_chopst(int);
void put_chopst(int);
void test(int);
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&room,0,1);
for(i=0;i<N;i++)
sem_init(&chopst[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("philospher %d is thinking \n ",i+1);
}
for(i=0;i<N;i++)
{
pthread_join(thread_id[i],NULL);
sem_destroy(&chopst[N]);
sem_destroy(&room);
pthread_exit(0);                            
}
}
void *philospher(void *num)
{
int x=1;
while(x<5)
{
int *i=num;
usleep(1);
take_chopst(*i);
usleep(1);
put_chopst(*i);
x++;
}
}
void take_chopst(int ph_num)
{
sem_wait(&room);
state[ph_num]=HUNGRY;
printf("philospher %d is hungry \n",ph_num+1);
test(ph_num);
sem_post(&room);
sem_wait(&chopst[ph_num]);
}
void test(int ph_num)
               {
if(state[ph_num]==HUNGRY && state[Left]!=EATING && state[Right]!=EATING)
{
state[ph_num]=EATING;
usleep(1);
printf("philospher %d takes left chopst %d and right chopst %d\n",ph_num+1,Left+1,ph_num+1);
printf("philospher %d is Eating \n",ph_num+1);
sem_post(&chopst[ph_num]);
}
}
void put_chopst(int ph_num)
{
               sem_wait(&room);
               state[ph_num]=THINKING;
               printf("philospher %d is putting left chopst %d and right chopst %d down\n",ph_num+1,Left+1,ph_num+1);
               printf("Philospher %d is thinking \n",ph_num+1);
               test(Left);
               test(Right);
               sem_post(&room);
}


A7 B.2

7(B.2) #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<fcntl.h> #in...