• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

ProjectsGeek

Set Operations program in C

April 3, 2011 by ProjectsGeek 2 Comments

Set Operations program in C

 

 

Set Operations program in CPrograms source code to perform Set Operations program in C and C++ Language. Operations on set to be performed are Given below

  • Input Operations – This operation should allow the user to provide input to the program. The program should check the provided input to see whether it’s valid or not if the input is not valid then the program should ask the user to provide input again.
  • Display Operations – This operation should present the user with the requested output. Display operations will have another sub-menu to select other options.
  • Union Operation – This operation of the program will implement the union operation on the provided input and then display the final output.
  • Intersection Operations – This option should implement intersection operation on the provided input and output the result. 
  • Difference Operations – This operation should implement a difference operation on the provided sets. After calculating the difference user should be provided with the output.
  • Symmetric Difference Operations – This option of the Set Operations program in C should implement symmetric difference operations to the sets.
  • Exit – Upon selecting this option, the program should quit.

These operations on sets are performed by implementing a simple logic in C language code. Users have to provide input to the Operations on Set program and it will print the selected operation on Set.

Set Operations Program Code 

 

#include  
 #include  
 int _union(int[],int[],int[],int,int);  
 int input(int[]);  
 int intersection(int[],int[],int[],int,int);  
 int difference(int[],int[],int[],int,int);  
 int symdiff(int[],int[],int[],int,int);  
 void display(int[],int);  
 void main()  
 {  
 int set1[20],set2[20],set3[40],s1,s2,s3,ch,ch1,ch2;  
 clrscr();  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t\t------: MENU :--------\n\n");  
 printf("\n\t1.\tINPUT\n");  
 printf("\n\t2.\tDISPLAY\n");  
 printf("\n\t3.\tUNION \n");  
 printf("\n\t4.\tINTERSECTION\n");  
 printf("\n\t5.\tDIFFERENCE\n");  
 printf("\n\t6.\tSYMMETRIC DIFFERENCE\n");  
 printf("\n\t7.\tEXIT\n");  
 printf("\n\n\n\t\tPLEASE ENTER UR CHOICE\t:- ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1 :  
 do{  
 clrscr();  
 printf("\n\n\n\t\t\t-------: INPUT MENU :-------\n\n");  
 printf("\n\t1.\tSET1\n");  
 printf("\n\t2.\tSET2\n");  
 printf("\n\t3.\tRETURN TO MAIN MENU\n");  
 printf("\n\t\tENTER UR CHOICE  :-  ");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 s1=input(set1);  
 break;  
 case 2:  
 s2=input(set2);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 2:  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t\t-------: OUTPUT MENU :-------\n\n");  
 printf("\n\t1.\tSET1\n");  
 printf("\n\t2.\tSET2\n");  
 printf("\n\t3.\tRETURN TO MAIN MENU\n");  
 printf("\n\t\tENTER UR CHOICE  :-  ");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 display(set1,s1);  
 break;  
 case 2:  
 display(set2,s2);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 3 :  
 if(s1==0 &&s2==0)  
 printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
 else  
 {  
 s3=_union(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 }  
 break;  
 case 4 :  
 if(s1==0 &&s2==0)  
 printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
 else  
 {  
 s3=intersection(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 }  
 break;  
 case 5 :  
 if(s1==0 &&s2==0)  
 {  
  printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
  break;  
 }  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t\t-------: DIFFERENCE MENU :-------\n\n");  
 printf("\n\t1.\tSET1 - SET2\n");  
 printf("\n\t2.\tSET2 - SET1\n");  
 printf("\n\t3.\tRETURN TO MAIN MENU\n");  
 printf("\n\t\tENTER UR CHOICE  :-  ");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1: s3=difference(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 break;  
 case 2: s3=difference(set2,set1,set3,s2,s1);  
 display(set3,s3);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 6:  
 if(s1==0 &&s2==0)  
 {  
  printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
  break;  
 }  
 s3=symdiff(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 break;  
 case 7: break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch!=7);  
 getch();  
 }   //END OF MAIN  
 /*****************************************************  
 INPUT FUNCTION  
 *****************************************************/  
 int input(int set[])  
 {  
 int i=0,j,s;  
 printf("\n\tENTER THE SIZE OFTHE SET\t");  
 scanf("%d",&s);  
 printf("\n\tENTER THE SET\t\t\n");  
 while(i<s) <br=""> {  
 scanf("%d",&set[i]);  
 for(j=0;j<i;j++) <br=""> if(set[i]==set[j])  
 {  
 printf("\n\n\t!!!! DUPLICATED ENTRY !!!!\n\n");  
 printf("\t\t----------: ENTER AGAIN");  
 break;  
 }  
 if(i==j)  
 i++;  
 }  
 return s;  
 }  
 /*********************************************************  
 UNION FUNCTION  
 *********************************************************/  
 int _union(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int s3,i,j;  
 for(i=0;i<s1;i++) <br=""> set3[i]=set1[i];  
 s3=i;  
 for(i=0;i<s2;i++) <br=""> {  
 for(j=0;j<s1;j++) <br=""> if(set1[j]==set2[i])  
 break;  
 if(j==s1)  
 set3[s3++]=set2[i];  
 }  
 return s3;  
 }  
 /*************************************************************  
  INTERSECTION FUNCTION  
 ***************************************************************/  
 int intersection(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int i,j,s3=0;  
 for(i=0;i<s1;i++) <br=""> {  
 for(j=0;j<s2;j++) <br=""> if(set1[i]==set2[j])  
 {  
 set3[s3++]=set1[i];  
 break;  
 }  
 }  
 return s3;  
 }  
 /****************************************************************  
 DIFFERENCE FUNCTION  
 ******************************************************************/  
 int difference(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int i,j,s3=0;  
 for(i=0;i<s1;i++) <br=""> {  
 for(j=0;j<s2;j++) <br=""> if(set1[i]==set2[j])  
 break;  
 if(j==s2)  
 set3[s3++]=set1[i];  
 }  
 return s3;  
 }  
 /**********************************************************  
  SYMDIFF FUNCTION  
 ***********************************************************/  
 int symdiff(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int s3=0,se1[20],se2[20],si1,si2;  
 si1=difference(set1,set2,se1,s1,s2);  
 si2=difference(set2,set1,se2,s2,s1);  
 s3=_union(se1,se2,set3,si1,si2);  
 return s3;  
 }  
 /*********************************************************  
  DISPLAY FUNCTION  
 **********************************************************/  
 void display(int set[],int size)  
 {  
 int i;  
 printf("\n\n\t{ ");  
 for(i=0;i<size;i++) <br=""> printf("\t%d",set[i]);  
 printf("\t} ");  
 getch();  
 }
Download Code Here

Other Projects to Try:

  1. Set operations – Union, Intersection, Difference, Symmetric Difference using C
  2. Matrix operations in c language
  3. String Operations in C Program
  4. Sparse Matrix Operations Code using C Langauge
  5. Matrix Operations in C Language

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Readers Writers Problem using Semaphores

April 3, 2011 by ProjectsGeek 2 Comments

Readers Writers Problem using Semaphores

 

Reader Priority problem

 

Readers Writers Problem using SemaphoresReaders Writers problem in operating system for solving synchronization problem can be solved. This program is for unix machine as their only you can run this code. This program demonstrate reader priority code for reader writer problem.
Problem statement for implement Readers Writers problem using semaphores with reader  priority using C language. Use mutex and semaphores to implement above problem in c language environment.

In the reader priority case, the reader will always be given priority to other processes. Writer has to wait every time if their is any read request to be completed. So by this simulation of readers writers problem we can see how actually this problem can be tackled in real time. 

In the context of concurrent programming, the “reader priority case” refers to a scenario where readers are given precedence over writers when accessing a shared resource, such as a database or a file. This means that if there are both read and write requests pending, the system will prioritize servicing the read requests first before attending to any write requests.

In this scenario, whenever a writer wants to access the shared resource, it has to wait if there are any ongoing read operations. This ensures that readers are not kept waiting indefinitely and that their access to the resource is not unnecessarily delayed by write operations.

Simulating the readers-writers problem allows us to observe how different strategies for managing access to shared resources perform in real-world scenarios. By implementing and analyzing various algorithms and techniques for handling concurrent read and write operations, we can gain insights into the trade-offs involved and determine the most efficient and fair approach for different applications.

In essence, through this simulation, we can understand and evaluate the mechanisms for managing reader and writer access to shared resources in a real-time environment, helping us design more robust and efficient concurrent systems.

Readers Writers Problem Code

 

 #include   
 #include   
 #include   
 void * reader(void *) ;  
 void *writer (void *) ;  
 sem_t wsem,mutex ;  
 int readcount=0 ;  
 main()  
 {  
  int a=1,b=1;  
     system("clear");  
  sem_init(&wsem,0,1) ;  
  sem_init(&mutex,0,1) ;  
  pthread_t r,w,r1,w1 ;  
  pthread_create(&r,NULL,reader,(void *)a);  
     a++;  
  pthread_create(&w1,NULL,writer,(void *)b);  
     b++;  
  pthread_create(&r1,NULL,reader,(void *)a);  
  pthread_create(&w,NULL,writer,(void *)b);  
  pthread_join(r,NULL);  
  pthread_join(w1,NULL);  
  pthread_join(r1,NULL);  
  pthread_join(w,NULL) ;  
  printf("main terminated\n");  
 }  
 void * reader(void * arg)  
 {  
  int c=(int)arg ;  
  printf("\nreader %d is created",c);  
     sleep(1);  
  sem_wait(&mutex) ;  
     readcount++;  
     if(readcount==1)  
         sem_wait(&wsem) ;  
  sem_post(&mutex) ;  
 /*Critcal Section */  
  printf("\n\nreader %d is reading\n ",c);  
  sleep(1) ;  
  printf("\nreader%d finished reading\n",c);  
 /* critical section completd */  
  sem_wait(&mutex) ;  
     readcount-- ;  
     if(readcount==0)  
         sem_post(&wsem) ;  
  sem_post(&mutex) ;  
 }  
 void * writer(void * arg)  
 {  
  int c=(int)arg ;  
  printf("\nwriter %d is created",c);  
     sleep(1);  
  sem_wait(&wsem) ;  
  printf("\nwriter %d is writing\n",c) ;  
  sleep(1);  
  printf("\nwriter%d finished writing\n",c);  
  sem_post(&wsem) ;  
 }

 Download Readers writers problem using semaphore

Other Projects to Try:

  1. Readers-writers problem using Semaphore
  2. Queue Operations using C Language
  3. Matrix Operations in C Language
  4. Breadth First and Depth First Search C Language
  5. To Perform File Handling in Java

Filed Under: Operating System Tagged With: OS Problems

Readers-writers problem using Semaphore

April 3, 2011 by ProjectsGeek Leave a Comment

Readers-writers problem using Semaphore

Objective 

In this post, we will see Readers-writers problem code using C Language and we will try to understand how to solve this problem.

 

Readers writers problem using SemaphoreReaders-writers problem in c is the Synchronization problem which can be solved by using this algorithm. This code is written to run on Linux or Unix machines. So for running this code, you must have UNIX system with c compiler installed on it then only you can run these programs. The main reason that we require the Unix system is that we can only test reader-writer problem on Unix system.

Readers-writers problem is Mutual Exclusion and Synchronization problem which can solve by semaphores and monitors. Here is the Source Code for writer priority using C under reader and writer problem. Reader-writer code can be implemented in C on Linux.

We have declared two variables named as reading count and write count which will count the read and write to control the synchronization problem. Only the read or write permission will be given based on these variables. So we can say that it is a way in which we should handle the reader and writer. 

Mutual Exclusion and Synchronization are like the traffic rules of computer programs, ensuring that multiple threads or processes play nicely together when using shared resources. Imagine a semaphore as a traffic light: it tells threads when to stop and when to go, preventing them from crashing into each other while accessing shared data. Monitors, on the other hand, are like gated communities for data, where access is carefully controlled through a single entrance. Inside, everyone follows the rules, taking turns and waiting patiently when needed. Both semaphores and monitors offer smart ways to keep our programs running smoothly, preventing chaos and ensuring that everyone gets their fair share of resources.

Reader Writer problem using Reader Priority code can be found here.

Readers writers problem Code


#include   
#include   
#include 

void * reader(void *) ;  
void *writer (void *) ;  
sem_t x,y,z,wsem,rsem ;  
int readcount=0 ;  
int writecount=0 ;  

main()  
{  
  int a=1,b=1;  
  system("clear");  
  sem_init(&wsem,0,1) ;  
  sem_init(&x,0,1) ;  
  sem_init(&rsem,0,1) ;  
  sem_init(&y,0,1) ;  
  sem_init(&z,0,1) ;  

  pthread_t r,w,r1,w1,r2,r3,w2,w3 ;  
  pthread_create(&r,NULL,reader,(void *)a);  
     a++;  
  pthread_create(&r1,NULL,reader,(void *)a);  
     a++;  
  pthread_create(&w,NULL,writer,(void *)b);  
     b++;  
   pthread_create(&w1,NULL,writer,(void *)b);  
     b++;  
  pthread_create(&r2,NULL,reader,(void *)a);  
     a++;  
  pthread_create(&w2,NULL,writer,(void *)b);  
     b++;  
  pthread_create(&r3,NULL,reader,(void *)a);  
     a++;  
  pthread_create(&w3,NULL,writer,(void *)b);  
  pthread_join(r,NULL);  
  pthread_join(r1,NULL);  
  pthread_join(w,NULL);  
  pthread_join(w1,NULL);  
  pthread_join(r2,NULL);  
  pthread_join(w2,NULL) ;  
  pthread_join(r3,NULL);  
  pthread_join(w3,NULL);  
  printf("main terminated\n");  
 }  

void * reader(void * arg)  
{  
   int c=(int)arg ;  
   printf("\nreader %d is created",c);  
   sleep(1);  
   sem_wait(&rsem);  
   sem_wait(&x) ;  
   readcount++;  
   if(readcount==1)  
         sem_wait(&wsem) ;  
   sem_post(&x) ;  
   sem_post(&rsem);  
   sleep(1)  

 /*Critcal Section */  

  printf("\n\nreader %d is reading\n ",c);  
  sleep(1) ;  
  printf("\nreader%d finished reading\n",c); 

 /* critical section completd */  
  sem_wait(&x) ;  
  readcount-- ;  
  if(readcount==0)  
     sem_post(&wsem) ;  
  sem_post(&x) ;  
 } 

void * writer(void * arg)  
{  
  int c=(int)arg ;  
  printf("\nwriter %d is created",c);  
  sleep(1);  
  sem_wait(&y) ;  
  writecount++ ;  
  if(writecount==0)  
      sem_wait(&rsem) ;  
  sem_post(&y) ;  
  sem_wait(&wsem) ;  
  printf("\nwriter %d is writing\n",c) ;  
  sleep(1);  
  printf("\nwriter%d finished writing\n",c);  
  sem_post(&wsem) ;  
  sem_wait(&y) ;  
  writecount-- ;  
  if(writecount==0)  
      sem_post(&rsem) ;  
  sem_post(&y) ;  
 }

In conclusion, this article provides an in-depth exploration of the Readers-Writers problem in C Language, offering valuable insights into concurrent resource management. Through detailed analysis and practical code examples, readers gain a solid understanding of synchronization techniques and deadlock prevention strategies. Readers-Writers problem in C, concurrent resource access, and synchronization techniques, this content aims to enhance visibility and accessibility for programmers seeking solutions to concurrency challenges. With this comprehensive guide, readers can master the complexities of concurrent programming in C, empowering them to develop robust and efficient systems.

Download Source Code

Please use the below link for downloading source code.

Download Source Code

 

Other Projects to Try:

  1. Readers Writers Problem using Semaphores
  2. Regular Expression to DFA Code in C Language
  3. Breadth First and Depth First Search C Language
  4. String Operations in C Program
  5. Hoffmans algorithm in C Language

Filed Under: Operating System Tagged With: OS Problems

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 133
  • Page 134
  • Page 135

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy