ui lab manual

21
Program on process creation and execution 1.(a) To display environment variables. #define Buf siz 1024 #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { char name[BUFSIZ],*vp; printf("Enter name of environmental variable:\n"); fgets(name,BUFSIZ,stdin); name[strlen(name)-1]='\0'; vp=getenv(name); if(vp==NULL) { printf(stderr,"I dont know the environmental variable\n"); exit(1); } printf("value of %s is %s \n",name,vp); return 0; } OUTPUT: Enter name of the environment variable : path

Upload: computerstudent

Post on 12-Nov-2014

42 views

Category:

Documents


0 download

DESCRIPTION

unix internals lab manual for JNTU-A

TRANSCRIPT

Page 1: UI Lab Manual

Program on process creation and execution1.(a) To display environment variables.#define Buf siz 1024#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main(void)

{

char name[BUFSIZ],*vp;

printf("Enter name of environmental variable:\n");fgets(name,BUFSIZ,stdin);

name[strlen(name)-1]='\0';

vp=getenv(name);

if(vp==NULL)

{

printf(stderr,"I dont know the environmental variable\n");exit(1);

}

printf("value of %s is %s \n",name,vp);

return 0;

}

OUTPUT:Enter name of the environment variable : pathValue of path is /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/05A7/bin

(b). To implement Different types of exec functions.#include<stdio.h>

Page 2: UI Lab Manual

main(int argc,char *argv[])

{

int pid;

char *args[2];

printf("ready to FORK\n");

pid=fork();

if(pid==0)

{

printf("I AM THE CHILD!!!\n");

args[0]="./pri";

args[1]="NULL";

execv("./pri",args);

printf("oppsss0\n");

}

else printf("I AM THE PARENT!!!\n");

wait(NULL);

}

OUTPUT:

Ready to FORK

I AM THE CHILD!!!

I AM THE PARENT!!!

Hello world

Page 3: UI Lab Manual

2.write a program a)To open a streamb)To read and write a streamc)To position a stream.

#include<stdio.h>#include<stdlib.h>#include<string.h>main(int arg,char *arr[]){FILE *Fs,*Ft;int cpos;char ch;if(arg!=3){printf("arguements missing!press key to exit");exit(0);}Fs=fopen(arr[1],"r");if(Fs==NULL){printf("cannot copy file!press key top exit");fclose(Fs);exit(0);}Fs=fopen(arr[2],"w");if(Fs==NULL){printf("cannot copy file !press key to exit");fclose(Fs);exit(0);}while(1){ch=getc(Fs);if(ch==EOF){break;}elseputc(ch,Ft);}printf("file copied sucessfully");cpos=ftell(Fs);printf("\n now the position of the sensor is on the source stream is at %d th charecter",cpos);

Page 4: UI Lab Manual

fseek(Fs,4,SEEK_SET);cpos=ftell(Fs);printf("\n after fseek the position now the position of the cursor in the first stream is at %d th charecter",cpos);fclose(Fs);

}

OUTPUT:./a.out hi.txt hii.txtFile is createdAfter fseek the position of the cursor in the first stream is at 8

3.Write a program to find whether a file is having read,write,execute ACCESS Permissions andalso check whether givenname is file or directory clearecho enter nameread nameif [!-z $ fname]thenif [-rr $ name -a -w $ name -a -x $ name]thenecho the file is having all permissioneilf[-r $ name -a -x $ name]thenecho the file has read and execute permissionelif [-r $ name -a -w $ name]then echo the file has read &write permissionselif [-w $name -a -x $name]echo the file has write and execute permissionif[-f $name]thenecho u entered file namecat $nameelif[-d $fname]thenecho u entered directory ls $nameelse echo u entered invalid name

Page 5: UI Lab Manual

OUTPUT:

Enter file name: hi.cThe file has read and write permissions U entered file name

How r u?

5.Write a program on file operations1.create a file2. add record to file 3. modify a records4,delete a records

clearch='y';while[$ ch='y']doclearecho 1.create the fileecho 2.displayecho 3.copyecho 4.removeecho enter your choiceread choicecase $ choice in 1) echo create the file echo enter the file name read fn vi $ fn echo file created;;2) echo enter the file name read fn1 vi $ fn1;;3 echo copy echo enter the source file read source echo enter the destination file read destination cp $ source $ destination echo the files are copied;;

Page 6: UI Lab Manual

4) echo remove echo enter the file name to delete read fname rn $ fname echo the file is removed;;*) echo u enterd the wrong choice;; esac echo do you want to continue(y/n) reach ch done OUTPUT:

1.create a file

2.display

3.copy

4.remove

Enter your choice 1

Enter the file name : ram.sh

file created

do u want to continue y

enter your choice 2

enter file name : ram.sh

hai

do u want to continue :y

enter ur choice 3

enter the source file : ram.sh

enter the destination file : ram1.sh

the files are copied

do u want to continue : y

Page 7: UI Lab Manual

enter ur choice 4

enter the file name : ram.sh

the file is removed

do u want to continue n

6.Write a program to create a chain processes.#include <stdio.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>int main() { pid_t mypid, childpid; int status; mypid = getpid(); printf("Hi. I'm the parent process. My pid is %d.\n", mypid); childpid = fork(); if ( childpid == -1 ) { perror("Cannot proceed. fork() error"); return 1; } if (childpid == 0) { printf("Child 1: I inherited my parent's pid as %d.\n", mypid); mypid = getpid(); printf("Child 1: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(), mypid);childpid = fork(); if ( childpid == -1 ) { perror("Cannot proceed. fork() error"); return 1; } if (childpid == 0){ printf("Child 2: I hinerited my parent's PID as %d.\n", mypid); mypid = getpid(); printf("Child 2: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(), mypid); childpid = fork(); if ( childpid == -1 ) { perror("Cannot proceed. fork() error"); return 1; } if (childpid == 0) { printf("Child 3: I hinerited my parent's PID as %d.\n", mypid);

Page 8: UI Lab Manual

mypid = getpid(); printf("Child 3: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(), mypid); sleep(10); return 12;}else return 15;}return 0;}}

OUTPUT:cc chain.c./a.outHi. I'm the parent process. My pid is 5906Child 1: I inherited my parent's pid as 5906. Child 1: getppid() tells my parent is 5907. My own pid instead is 5907

Child 2: I inherited my parent's pid as 5908. My own pid instead is 5909Child 2: I inherited my parent's pid as 5908.Child 2: getppid() tells my parent is 5908. My own pid instead is 5909

7. write a program to a) create the semaphoresb) set values to semaphoresc)remove semaphores.

#include<stdio.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#include<unistd.h>#include<stdlib.h>#include<time.h> #define NS 3union semun{int val; struct semid_ds *buf; ushort *array; struct seminfo *_buf;};main(void){ int sem_id, sem_value, i; key_t ipc_key; struct semid_ds sem_buf; static ushort sem_array[NS] = {3,1,4};

Page 9: UI Lab Manual

union semun arg;ipc_key = ftok(".", 'S'); if (( sem_id = semget (ipc_key, NS, IPC_CREAT | 0666))==-1) { perror("semget: IPC_CREAT | 0666"); exit (1); } printf("Semaphore identifier %d\n", sem_id); arg.buf= &sem_buf; if (semctl(sem_id, 0, IPC_STAT, arg) == -1) { perror("semctl:IPC_STAT"); exit (2); }printf("Created %s", ctime(&sem_buf.sem_ctime)); arg.array=sem_array; if (semctl(sem_id, 0, SETALL, arg) == -1) {perror("semctl: SETALL"); exit (3); }for (i = 0; i < NS; ++i) { if ((sem_value = semctl(sem_id, i, GETVAL, 0)) == -1) {perror("semctl: GETVAL"); exit (4);} printf("Semaphore %d has value of %d\n", i, sem_value);}if (semctl(sem_id, 0, IPC_RMID, 0) == -1) { perror("semctl: IPC_RMID");exit (5);}}

output

$ cc sem.c

./a.out.sem.c

Semaphore identifier 884738

Created mar 27 14:42:39 2012

Semaphore 1 has value 3

Semaphore 2 has value 1

Semaphore 3 has value 4

8.Write a program to implement various operations on message queues.a)Server program.

#include<sys/types.h>#include<sys/ipc.h>

Page 10: UI Lab Manual

#include<sys/msg.h>#include<stdio.h>#define MSGSZ 128typedef struct msgbuf{long mtype;char mtext[MSGSZ];}message_buf;main(){int msqid;int msgflg=IPC_CREAT|0666;key_t key;message_buf sbuf;size_t buf_length;key=1234;if((msqid=msgget(key,msgflg))<0){perror("msgget");exit(1);}elsesbuf.mtype=1;(void)strcpy(sbuf.mtext,"did u get this?");buf_length=strlen(sbuf.mtext+1);if(msgsnd(msqid,&sbuf,buf_length,IPC_NOWAIT)<0){printf("%d,%d,%s,%d \n",msqid,sbuf.mtype,sbuf.mtext,buf_length);perror("msgsnd");exit(1);}elseprintf("message:\"%s\"sent\n",sbuf.mtext);exit(0);}

OUTPUT:

cc server.c_o siri

% siri&

[1] 11839

Message:Did you get this?sent

Page 11: UI Lab Manual

[1]+done ./ siri

bClient program.

#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<stdio.h>#define MSGSZ 128typedef struct msgbuf{long int mtype;char mtext[MSGSZ];}message_buf;main(){int msqid; key_t key;message_buf rbuf;key=1234if((msqid=msgget(key,0666))< 0 ){perror("msgget");exit(1);}

if(msgrcv(msqid,&rbuf,MSGSZ,1,0)<0){

perror("msgrcv");exit(1);}printf("%s\n",rbuf.mtext);exit(0);}

OUTPUT:

cc client.c

./a.out

Did you get this?

Page 12: UI Lab Manual

9. Write a program to demonstrate a) signal handlingb)terminal i/o.

#include <stdio.h>#include <signal.h>void sighup(); void sigint();void sigquit();main(){ int pid;if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { signal(SIGHUP,sighup);signal(SIGINT,sigint);signal(SIGQUIT, sigquit); for(;;); }else { printf("\nPARENT: sending SIGHUP\n\n"); kill(pid,SIGHUP); sleep(3); printf("\nPARENT: sending SIGINT\n\n"); kill(pid,SIGINT); sleep(3); printf("\nPARENT: sending SIGQUIT\n\n"); kill(pid,SIGQUIT); sleep(3); }}void sighup(){ signal(SIGHUP,sighup);printf("CHILD: I have received a SIGHUP\n");}void sigint(){ signal(SIGINT,sigint); printf("CHILD: I have received a SIGINT\n");}

Page 13: UI Lab Manual

void sigquit(){ printf("My DADDY has Killed me!!!\n");exit(0);}

OUTPUT: PARENT: sending SIGHUPCHILD: I have received a SIGHUPPARENT: sending SIGINTCHILD: I have received a SIGINTPARENT: sending SIGQUITCHILD: I have received a SIGQUIT

11.Write a program toa) create a shared memoryb) write to shared memoryc) read from shared memory.

#include<sys/types.h>#include<sys/ipc.h>#include<sys/shm.h>#include<unistd.h>#include<string.h>#include<errno.h>#include<stdlib.h>#include<stdio.h>int main(void){ pid_t pid; int *shared; int shmid; shmid=shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666); printf("shared memory ID= %u",shmid); if(fork()==0){ shared=shmat(shmid,(void *)0,0); printf("child pointer %u\n",shared); *shared=1; printf("child value=%d\n",*shared); sleep(2); printf("child value=%d\n",*shared);

Page 14: UI Lab Manual

}else{ shared=shmat(shmid,(void *)0,0); printf("\nparent pointer %u\n",shared); printf("parent value=%d\n",*shared); sleep(1); *shared=42; printf("parent value=%d\n",*shared); sleep(5); shmctl(shmid,IPC_RMID,0);}}

OUTPUT :

Cc shmem.c./a.outShared memory ID=243543Child pointer 308654654Child value=1Parent pointer 308654654Parent value=1Parent value=42Child value =42

12. Write a program to create two pipes.#include<stdio.h>#include<sys/types.h>#include<stdlib.h>#include<unistd.h>int main(){int pid,pfd1[2],pfd2[2];char line1[80],line2[80];system("clear");if(pipe(pfd1)<0)printf("\n pipe creation error");if(pipe(pfd2)<0)printf("\n pipe creation error");if(!fork()){printf("\n child :reading from parent");read(pfd1[0],line1,80);write(pfd2[1],"\n hello i am child",80);printf("\n read from parent : %s",line1);exit(0);}

Page 15: UI Lab Manual

else{printf("\nparent :writing to child\n");write(pfd1[1],"\n hai i am parent",80);

wait(NULL);

read(pfd2[0],line2,80);

printf("\n read from child :%s",line2);

wait(NULL);

}

}

OUTPUT:

parent:Writing to the child

child:Reading from parent

child:Writing to the parent

parent:Reading from child

13.write a shell program that takes a command line argument & reports in whether it is a directory or a file.clearecho enter the file nameread fnameif [-f $ fname]thenecho you entered file namecat $ fnameelif[-d $ fname]then echo you entered a directory namels $ fnameelseecho you entered invaild namefi

OUTPUT:

Enter file name: hi.c

Page 16: UI Lab Manual

U entered file name

How r u?

14.Write program to implement word count.clearecho enter the wordread word echo enter the file nameread fname echo the number of timer the word occured in this filegrep_c $ word $ fname

OUTPUT:Sh word.shEnter the wordEnter the filenameThe number of times the word occurred in this file is 3.

15.Write a program to find the following information of the filea) File typeb) Number of linksc) Read & write & execte permissionsd) Given name is file or directory.

#include<sys/types.h>#include<unistd.h>#include<fctnl.h>#include<time.h>#include<sys/stat.h>#include<stdio.h>main(){char fname[20];struct stat buf;printf("enter the file name");scanf("%s",fname);stat(fname,&buf);printf("i node no:%d \n",buf.st_ino);printf("no of links:%d \n",buf.st_n link);printf("size of the file in bytes %d \n",buf.st_size);printf("access time :%d",buf.st_a time);printf("modification time :%d \n",buf.st_m time);printf("change time : %d",buf.st_c time);}

Page 17: UI Lab Manual

16.Write a program to check whether both files are same or not.clearecho enter the first file nameread fname1echo enter the second file nameread fname2echo comparison of two filesif cmp $ fname1 $ fname2thenecho two files are equalrm $ fname1cat fname2elseecho two files are not equalcat fname1cat fname2fi

OUTPUT:

enter the first file name ram.shenter the second file name ram1.shcomparision of two files

two files are equal

this is C.S.E

b.To write a shall program that convert lower case to upper case string.Clear Echo enter the file name with pathRead fname1Echo enter the new file nameRead fname2tr ‘[a-z]’’[A-Z]’ <$fname1> $fname2echo The data is coverted to upper casecat $fname2echo The file exist in the current directory

17.Write a shall program to perform the following operations.

Page 18: UI Lab Manual

a) To extract a sub string from a given string.b) To find the length of a given string.

ClearCh=’y’While[$ch==’y’]DoClearEcho MAIN MENUEcho1->To find the length of the stringEcho2->To extract the substring from a given stringEcho Enter the string to perform operationsRead strEcho enter your choiceRead choiceCase $choice1)echo you selected to find the length of the string Echo The length of the string is:$[#str] ; ;2)Echo you selected to extract the substring. Echo enter the starting position to view the substring Read ch1Echo enter the ending position toview substring Read ch2Echo ${str:ch1.$ch2}*)echo your option is incorrect; ;EsacEcho do you want to continue?Read chDone

Page 19: UI Lab Manual