Thread: Interrupts on threads

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    Interrupts on threads

    I want to know how I can make interrupts on threads.

    Example:
    1 thread is using 2 ressources of the same kind. Then comes a second thread which needs to use 1 of the same kind of ressources..

    How can I interrupt the first thread, so that the second thread can get the ressource needed?

    much thanks

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    by the way, it's linux programming with c

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    I just need to know where I can read about my problem. I dont request anybody to actually write code.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    look for "semiphores", which synchronizes the use of objects between threads. When the program starts it creates a semaphone. Then when one thread needs access to an object it first requests access to the semaphone from the operating system. If no other threads are using the semaphore the os grants access, otherwise if another thread is using the semaphone the os blocks the thread until either a timeout expires or the semaphone is released by the other thread.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    I made made this following code:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    
    #include "patientStruct.h"
    
    #define LOGFILE		"logFil.txt"
    
    sem_t dame1;
    skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade)
    
    
    int main(int argc, char *argv[])
    {
    	sem_init(&dame1,0,1);
    	int i;
    	
    
    	while(i=0; i < Patient[MAX]; i++) //sålænge et eller andet mht. patienterne..
    	{
    		printf("Patient %d bliver nu modtaget af dame1\n", i, 0);
    
    		//Kritisk område start
    		sem_wait(&dame1);
    
    			printf("Dame1 har skrevet Patient %d i log'en\n", i);
    			skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade) // Here I want to write         to logfil.txt
    			
    			sleep(Random(2)+2);
    
    		sem_post(&dame1);
    		//Kritisk område slut
    		
    	}
    }
    
    /*
    	Skriver tekst til logFil.txt, samt til skærm
    */
    void skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade)
    {
    	FILE *fp = fopen(LOGFILE, "a");
    	fprintf(fp, p[MAX].cprnr, p[MAX].prio, p.[MAX].skade);
    	fclose(fp);
    }
    But I need 2 processes, who constantly take threads, who are in a queue, and write their nformation to logfil.txt.
    How do I make 2 processes, who communicate with a queue of threads, with the above semaphore?

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    each thread is a patient, for whom the variables cprnr, prio and skade are defined in patientStruct.h

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Unless I'm missing something, and to be honest I don't work with threads much, but you are not creating any threads.

    You just seem to have a single process that loops around, using semaphores to wait and post.

    You've include pthread.h, but you're not calling pthread_create or anything like that.

    Perhaps something like this might help: pthread tutorial

    Good luck.....

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Yeah, I dont create any threads, because they are created elsewhere. This code, should just let each thread enter, write to logifl.txt and then leave, allowing the next thread to do the same

  9. #9
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by philipsan
    Yeah, I dont create any threads, because they are created elsewhere. This code, should just let each thread enter, write to logifl.txt and then leave, allowing the next thread to do the same

    Okay, my bad.....

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    I made this code now:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <sys/types.h>
    
    #include "patientStruct.h"
    
    #define LOGFILE		"logFil.txt"
    
    sem_t dame1;
    skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade)
    
    
    int main(int argc, char *argv[])
    {
    	sem_init(&dame1,0,1);
    	int i, n;
    	pid_t pid;
    	
    	pid = fork();
    	
    	switch(pid)
    	{
    	case -1:
    		perror("fork failed!");
    		exit(1);
    	case 0:
    	
    		while(i=0; i < Patient[MAX]; i++) //sålænge et eller andet mht. patienterne..
    		{
    		printf("Patient %d bliver nu modtaget af dame1\n", i, 0);
    		
    		//Kritisk område start
    		sem_wait(&dame1);
    
    			printf("Dame1 har skrevet Patient %d i log'en\n", i);
    			skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade)
    			
    			sleep(2);
    
    		sem_post(&dame1);
    		//Kritisk område slut
    		}
    	default:
    		while(i=0; i < Patient[MAX]; i++) //sålænge et eller andet mht. patienterne..
    		{
    		printf("Patient %d bliver nu modtaget af dame1\n", i, 0);
    		
    		//Kritisk område start
    		sem_wait(&dame1);
    
    			printf("Dame1 har skrevet Patient %d i log'en\n", i);
    			skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade)
    			
    			sleep(2);
    
    		sem_post(&dame1);
    		//Kritisk område slut
    		}
    		break;
    	}
    	exit(0);	
    }
    
    /*
    	Skriver tekst til logFil.txt, samt til skærm
    */
    void skrivTilLog(p[MAX].cprnr, p[MAX].prio, p.[MAX].skade)
    {
    	FILE *fp = fopen(LOGFILE, "a");
    	fprintf(fp, p[MAX].cprnr, p[MAX].prio, p.[MAX].skade);
    	fclose(fp);
    }
    I need this code to go through all the patients(threads) via their information, which lies in my struct.

    Is this somewhat more correct?
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM