Uncategorized

Write a recursive program to find the solution of placing n queens on chessboard so that no two queens attack each other using Backtracking.

#include<stdio.h> void nqueen(int k, int n); int place(int k,int i); int x[10],n; int main() { int i,j,k; k=1; printf("\nEnter NO Of Queens:-"); scanf("%d",&n); nqueen(k,n); } void nqueen(int k,int n) { int i,j; for(i=1;i<=n;i++) { if(place(k,i)) { x[k]=i; if(k==n) { printf("\n solution: "); for(j=1;j<=n;j++) { printf("%d\t", x[j]); } } else nqueen(k+1,n); } } } int place(int… Continue reading Write a recursive program to find the solution of placing n queens on chessboard so that no two queens attack each other using Backtracking.

Advertisements
Uncategorized

pipe

#include<stdio.h> #include<string.h> #include<sys/types.h>   int main() { FILE *fp; char ch,data[100],data1[100],data2[100],filename[100]; int pid,p,i=0; int fd1[2],fd2[2]; pipe(fd1); pipe(fd2); pid=fork(); if(pid==0) { sleep(1); close(fd1[1]); read(fd1[0],data,100); printf("\nFilename=%s\n",data); fp=fopen(data,"r"); while((ch=fgetc(fp))!=EOF) { data1[i]=ch; i++; } data1[i]='\0'; printf("\nDATA1:%s\n",data1); close(fd2[0]); write(fd2[1],data1,strlen(data1)+1); fclose(fp); } else { printf("\nEnter text file name:"); scanf("%s",filename); close(fd1[0]); write(fd1[1],filename,strlen("filename")+1); sleep(1); wait(0); close(fd2[1]); read(fd2[0],data2,100); printf("\nContents of file are:%s\n",data2); } return… Continue reading pipe

Uncategorized

Interprocess Communication

client.c #include<sys/ipc.h> #include<sys/shm.h> #include<stdio.h> #define shmsz 128 int main() { char *shm; int shmid; key_t key=678; shmid=shmget(key,shmsz,0666); if(shmid<0) { printf("\nError occured"); } shm=shmat(shmid,NULL,0); while(*shm!='\0') { putchar(*shm); *shm++; } } Server.c #include<sys/ipc.h> #include<sys/shm.h> #include<stdio.h> #define shmsz 128 int main() { char *shm; int shmid,i=0; char str[100]="VPKBIET" ; key_t key=678; shmid=shmget( key,shmsz,IPC_CREAT |0666); if(shmid<0) { printf("\nError occured");… Continue reading Interprocess Communication

Uncategorized

Orphan AND Zombie Process

ORPHAN PROCESS   #include<sys/types.h> #include<stdio.h> int main() { int pid; pid=fork(); if(pid==0) { printf("\n child process id=%d",(int)getpid()); printf("\n parent of child process id=%d",(int)getppid()); system( "ps -l | grep -w a.out"); sleep(10); printf("\n child process id=%d",(int)getpid()); printf("\n parent of child process id=%d",(int)getppid()); system( "ps -l | grep -w a.out"); } else { printf("\n parent of child… Continue reading Orphan AND Zombie Process

Uncategorized

file handling using low level system calls

  #include<fcntl.h> #include<stdio.h> #include<stdlib.h> #include<string.h> void createfile() { char s[20]; int fp; fp=open("abc.txt",O_WRONLY); printf("\n enter something:"); scanf("%s",s); if(fp<0) printf("\n error"); else { printf("\n file ceated sucefully"); write(fp,s,strlen(s)+1);   } close(fp);   } int getpointer() { int fp,curr; fp=open("abc.txt",O_RDWR); curr=lseek(fp,0,SEEK_END); write(fp,"hello",5); close(fp); return curr;   } void print() { char s1[100]; int fp; fp=open("abc.txt",O_RDONLY); read(fp,&s1,100); printf("\n… Continue reading file handling using low level system calls

Uncategorized

fifo: full duplex communication between two process

process1.c   #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #include<string.h> int main() { int f1,f2; char str[256]; char buf[100],str1[100]; f1 = mkfifo("fifo1",0666); printf("mkfifo: %d",f1); printf("Enter a String: \n"); fgets(str1,100,stdin); //scanf("%s",&str1); //strcat(str1,"\0"); f1=open("fifo1",O_RDWR); write(f1,str1,100); f2=open("fifo2",O_RDWR); read(f2,buf,100); printf("\n%s \n",buf); close(f1); close(f2); return 0; }   Process2.c #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #include<string.h> int main() { int f1,f2,i=0,space=0; char str[100]; char buf[100],ch; int chart,… Continue reading fifo: full duplex communication between two process

Uncategorized

deadlock solution for dining philosopher’s problem

  #include<stdio.h> #include<pthread.h> pthread_mutex_t chop[5]; int i,k; char status[5]={'-','-','-','-','-'}; void display() { printf("\n"); for(k=0;k<5;k++) printf(" %c ",status[k]); printf("\n"); } void* philosopher(void *n) { int num=(int)n; // printf("\nPhilosopher %d is hungry",num); status[num]='H'; display(); pthread_mutex_lock(&chop[num]); pthread_mutex_lock(&chop[(num+1)%5]); //printf("\nPhilosopher %d is eating",num); status[num]='E'; display(); sleep(2); pthread_mutex_unlock(&chop[(num+1)%5]); pthread_mutex_unlock(&chop[num]); // printf("\nPhilosopher %d is thinking",num); status[num]='T'; display(); }   int main() {… Continue reading deadlock solution for dining philosopher’s problem

Operating systems, Uncategorized

Write a program to demonstrate Reader-Writer problem with reader priority.

  /* Write a program to demonstrate Reader-Writer problem with reader priority.*/ Theory There is a shared resource which should be accessed by multiple processes. There are two types of processes in this context. They are reader and writer. Any number of readers can read from the shared resource simultaneously, but only one writer can… Continue reading Write a program to demonstrate Reader-Writer problem with reader priority.

Operating systems, Uncategorized

Implement a solution for producer-consumer problem with counting semaphores and mutex.

/*Implement a solution for producer-consumer problem with counting semaphores and mutex.*/ Theory Producer-Consumer The producer’s job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer), one piece at a time. The problem is to make sure… Continue reading Implement a solution for producer-consumer problem with counting semaphores and mutex.

Operating systems, Uncategorized

Implement matrix multiplication using multithreading. Application should make use of pthread create, pthread join, pthread exit

    /*Implement matrix multiplication using multithreading. Application should make use of pthread create, pthread join, pthread exit. Program should also • Collect the returned value in main function using pthread join() • Display sum of row column multiplication by main thread (main func- tion).*/ Theory Thread of execution is the smallest sequence of programmed… Continue reading Implement matrix multiplication using multithreading. Application should make use of pthread create, pthread join, pthread exit