Skip to main content

Posts

Showing posts with the label C programming

C Program to show Memory Allocation

C Program to show Memory Allocation # include <fcntl.h> # include <stdio.h> # include <string.h> # include <sys/types.h> # include <unistd.h> # include <stdlib.h> int main ( ) { int fd , fp , ch , l = 0 , q = 0 ; char * name , * in , * out ; printf ( "Enter the name of the file:" ) ; scanf ( "%s" , name ) ; fd = open ( name , O_CREAT , S_IRWXU ) ; if ( fd != - 1 ) { printf ( "%s File is created.\n" , name ) ; close ( fd ) ; while ( q == 0 ) { printf ( "Menu\n1.Rdonly\n2.Wronly\n3.Rdwr\n4.Exit\nEnter your choice:" ) ; scanf ( "%d" , & ch ) ; switch ( ch ) { case 1 : fp = open ( name , O_RDONLY ) ; printf ( ...

C Program to show an example of Process Management

C Program to show an example of Process Management   # include <unistd.h> # include <errno.h> # include <stdio.h> # include <stdlib.h> int main ( ) { pid_t childpid ; int retval ; int status ; childpid = fork ( ) ; if ( childpid >= 0 ) { if ( childpid == 0 ) { printf ( "CHILD : I am the child process\n" ) ; printf ( "CHILD : Here's my PID : %d\n" , getpid ( ) ) ; printf ( "CHILD : My parent's PID is : %d\n" , getpid ( ) ) ; printf ( "CHILD : The value of my copy of childpid is :%d\n" , childpid ) ; printf ( "CHILD : Sleeping for 4 seconds...\n" ) ; sleep ( 4 ) ; printf ( "CHILD : Enter an exit value (0 to 255): " ) ; scan...

C Program to show and example for FCFS scheduling

C Program to show and example for FCFS scheduling   # include <stdio.h> void main ( ) { int i , k , s = 0 , r = 0 , x = 0 , a [ 30 ] , t [ 30 ] , w [ 30 ] , e [ 30 ] ; float n , m , p = 0 ; printf ( "Enter the number of process: " ) ; scanf ( "%f" , & n ) ; printf ( "Enter the execution time: " ) ; for ( i = 0 ; i < n ; i ++ ) { printf ( "i=%d n=%f\n" , i , n ) ; scanf ( "%d" , & a [ i ] ) ; e [ i ] = a [ i ] ; } printf ( "After First come First serve scheduling: \n" ) ; w [ 0 ] = 0 ; t [ 0 ] = a [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { w [ i ] = t [ i - 1 ] ; t [ i ] = t [ i - 1 ] + a [ i ] ; } printf ( "Process \tBurst Time \tTurn Around Time \n" ) ; float tt = 0 , wt = 0 ; for ( i...

C Program to show an example of Fork System Call

  C Program to show an example of Fork System Call # include <stdio.h> # include <sys/types.h> # include <unistd.h> # include <stdlib.h> int main ( void ) { pid_t pid = fork ( ) ; if ( pid == 0 ) { int j ; for ( j = 0 ; j < 10 ; j ++ ) { printf ( "Child:%d\n" , j ) ; sleep ( 1 ) ; } exit ( 0 ) ; } else if ( pid > 0 ) { int i ; for ( i = 0 ; i < 10 ; i ++ ) { printf ( "Parent:%d\n" , i ) ; sleep ( 1 ) ; } } else { printf ( "Couldn't fork" ) ; exit ( 1 ) ; } }

C Program to show an example of Memory Management

  C Program to show an example of Memory Management # include <fcntl.h> # include <sys/types.h> # include <stdio.h> # include <unistd.h> # include <sys/resource.h> void main ( void ) { printf ( "\n The current location of the program break: %d\n" , ( int ) sbrk ( 0 ) ) ; int * t = sbrk ( 200 ) ; printf ( "\n After incrementing program's data space by 200 bytes using sbrk() system call" ) ; printf ( "\n The current location of program break: %d\n" , ( int ) sbrk ( 0 ) ) ; printf ( "\n Setting the end of data segment using brk() system call" ) ; brk ( t ) ; printf ( "\n The current location of the program break:%d\n" , ( int ) sbrk ( 0 ) ) ; }

C Program to show an example of Indirect Communication

  C Program to show an example of Indirect Communication recieve : # include <stdio.h> # include <stdlib.h> # include <errno.h> # include <sys/types.h> # include <sys/msg.h> struct my_msgbuf { long mtype ; char mtext [ 200 ] ; } ; int main ( void ) { struct my_msgbuf buf ; int i = ftok ( "snd.c" , 'B' ) ; int msqid = msgget ( i , 0644 ) ; for ( ; ; ) { msgrcv ( msqid , & buf , sizeof ( buf . mtext ) , 0 , 0 ) ; printf ( "Recieving from message queue:%s\n" , buf . mtext ) ; } return ( 0 ) ; } send : # include <stdio.h> # include <stdlib.h> # include <errno.h> # include <string.h> # include <sys/types.h> # include <sys/msg.h> # include <sys/ipc.h> struct my_msgbuf { long mtype ; char mtext [ 200 ] ; } ; int main ( void ) { struct my_msgbuf buf ; int i = ftok ( "snd.c" , 'B' ) ; int msqid = msgget ( i , 0644 | IPC_CREAT ) ; printf ( "Enter the mess...