Like Tree2Likes
  • 2 Post By cas

Help with segmentation fault!!!

This is a discussion on Help with segmentation fault!!! within the C Programming forums, part of the General Programming Boards category; Can someone please help me with this problem. I can't seem to find out why this segmentation fault happens. I ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Question Help with segmentation fault!!!

    Can someone please help me with this problem. I can't seem to find out why this segmentation fault happens. I really need this done by today. Anyone can help? Thanks in advanced and sorry of my english is not the best.

    If you want to compile just use gcc -Wall -DREENTRANT T2E3.c -lpthread -o T2E3

    T2E3.c is the file containing the code below and the input.asc used here is just a file containing these numbers:
    Code:
    1.3
    8.34
    56.43
    86.25
    69.69
    0.44
    2.643
    0.86
    567.86
    39.7
    10.4
    79.31



    Best Regards to my fellow devolopers

    Tiago




    Code:
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <float.h>
    #include <pthread.h>
    #include <sys/mman.h>
    #include <semaphore.h>
    
    
    #define Flags (O_CREAT | O_EXCL)
    #define Perms 0777
    
    
    int *client();
    void *server();
    
    
    typedef struct{
    	FILE *f_in_bin;				
    	int fd_in_asc;
    	int fd_out_asc;
    } file_descriptors;
    
    typedef struct {
    		int pos;
    		int null;
    		int neg;
    } tabela;
    
    tabela *tab;
    
    pthread_t *pthread_client,*pthread_server;
    pthread_attr_t attr;
    
    
    char *addr;
    int fd_mem;
    
    
    sem_t *semid[2];
    int value;
    char *nome_sem_1="sem_file_1";
    char *nome_sem_2="sem_file_2";
    int Sem_Val=0;
    
    
    int alpha=0;
    
    
    
    int main(){
    	
    	
    
    	fd_mem = open("sh_mem",O_RDWR|O_TRUNC|O_CREAT,Perms);
    	addr = mmap(NULL, sizeof(float), PROT_NONE , MAP_PRIVATE , fd_mem, 0);
    
    	
    	semid[0] = sem_open(nome_sem_1, Flags, Perms,Sem_Val);
    	semid[1] = sem_open(nome_sem_2, Flags, Perms,Sem_Val);
    
    
    	pthread_attr_init(&attr);
    	
    	file_descriptors *fd;		
    				
    	
    	fd=(file_descriptors*)malloc(sizeof(file_descriptors)); 
    	
    	
    	int fd_i_asc = open("input.asc",O_RDONLY,0777);
    	fd->fd_in_asc=fd_i_asc;						
    	
    
    	
    	int fd_o_asc = open("output.asc",O_WRONLY|O_CREAT,0777);
    	fd->fd_out_asc=fd_o_asc;										
    	
    
    	FILE *f_i_bin=fopen("input.bin","w+");
    	fd->f_in_bin =f_i_bin;									
    	
    	
    	pthread_client = (pthread_t*)malloc(sizeof(pthread_t));
    	pthread_server = (pthread_t*)malloc(sizeof(pthread_t));
    
    	pthread_create(pthread_client,&attr,(void*)client,(void*)fd);			
    	sleep(1);
    
    	pthread_create(pthread_server,&attr,server,(void*)fd);
    	
    	close(fd_i_asc);
    	fclose(f_i_bin);
    	close(fd_o_asc);
    
    	printf("Nº de Positivos: %i\nNº de Nulos: %i\nNº de Negativos: %i\n",(int)((tabela*)tab)->pos,(int)((tabela*)tab)->null,(int)((tabela*)tab)->neg);
    
    	pthread_attr_destroy(&attr);
    	free(fd);						
    	free(pthread_client);
    	free(pthread_server);
    
    	if(munmap(addr,sizeof(float))==-1) printf("Error unmaping the address");
    	if(sem_close(semid[0])==-1)printf("Error closing the semaphore 1");
    	if(sem_close(semid[1])==-1)printf("Error closing the semaphore 2");
    	if(sem_unlink(nome_sem_1)==-1)printf("Error unlinking the semaphore 1");
    	if(sem_unlink(nome_sem_2)==-1)printf("Error unlinking the semaphore 2");
    	return 0;
    
    }
    int *client(void *fd){
    	
    	float var;
    	tabela *tab;
    	tab=(tabela*)malloc(sizeof(tabela*)); 
    	tab->pos=0;
    	tab->null=0;
    	tab->neg=0;
    		
    	while(read((int)(((file_descriptors*)fd)->fd_in_asc),(void *) &var,sizeof(float))>0){	
    	
    
    		
    		
    		if(var<0.0)tab->neg++;
    		if(var>0.0)tab->pos++;
    		if(var==0.0)tab->null++;	
    		while(sem_post(semid[1])==-1) printf("Cliente:  Erro no post do 1\n");	
    		while(sem_wait(semid[0])==-1) printf("Cliente:  Erro no wait do 2\n");		
    		
    		
    		write(fd_mem,&var,sizeof(float));
    		
    	}
    	
    	while(sem_post(semid[1])==-1) printf("Cliente:  Erro no post do 1\n"); //<----- SEGMENTATION FAULT HERE
    			
    	while(sem_wait(semid[0])==-1) printf("Cliente:  Erro no wait do 2\n");	
    	
    	
    	var=FLT_MAX;	
    	write(fd_mem,&var,sizeof(float));											
    
    
    	return (void*)((tabela*)(tab));							
    	
    
    }
    
    void *server(void *fd){
    
    	float numero;
    	int n=0;
    
    	while(1){	
    		
    	if(sem_wait(semid[1])==-1) printf("Servidor:  Erro no wait do 1\n");	
    	read(fd_mem,&numero,sizeof(float)>0);	
    		if (read(fd_mem,&numero,sizeof(float))>0){
    	if(sem_post(semid[0])==-1) printf("Servidor:  Erro no post do 0\n");
    			if(numero==FLT_MAX) break;
    			
    			
    			write((int)(((file_descriptors*)fd)->f_in_bin),&numero,sizeof(float));
    			
    
    			n++;
    
    		}
    		else break;
    	
    	}
    
    
    	
    
    	int j;	
    	float tab[n];
    	
    	fread(tab,sizeof(float),n,(FILE*)(((file_descriptors*)fd)->f_in_bin));
    	
    	for(j=0; j<n; j++) 
    		tab[j] *= 3.0;
    	
    	
    
    	for( j=0; j<n; j++) 
    		write((int)(((file_descriptors*)fd)->fd_out_asc),&tab[j],sizeof(float));				
    	
    		
    
    	return 0;
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    How about you run it in the debugger (gdb)? It will stop when the seg fault occurs.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    You should not be casting so much:
    Code:
    pthread_create(pthread_client,&attr,(void*)client,(void*)fd);
    Why cast client to void*? You're doing it, I suspect, because you got a warning when you didn't use the cast. That's because client is the wrong type. You need to make it:
    Code:
    void *client(void *);
    Which also means never use just () in a function declaration. That means "unspecified number of parameters", not "no parameters". Always specify the types of your parameters.

    The cast of fd to void* is pointless, too. fd will automatically be converted to void*, no intervention necessary. Casting is rarely useful, and generally succeeds only in telling the compiler you know what you're doing, when you don't:
    Code:
    write((int)(((file_descriptors*)fd)->f_in_bin),&numero,sizeof(float));
    Why the cast to int? Is it because your compiler told you that there was a problem converting FILE* to int? There is a problem: it has no meaning! Casting just tells the compiler to shut up, it doesn't fix the bug. File descriptors and FILE pointers are not magically interchangeable. You might be interested in fwrite() or fileno(), but you should absolutely not cast a FILE* to an int.

    Remove all your casts, including those on malloc(). Fix any diagnostics that pop up, but do not fix them with casts. You also, sometimes, are casting a type to itself, such as:
    Code:
    fread(tab,sizeof(float),n,(FILE*)(((file_descriptors*)fd)->f_in_bin));
    f_in_bin is a FILE*, so why would you try to convert it to a FILE*? These types of casts add nothing but clutter to the code.

    But what you might especially be interested in is:
    Code:
    tab=(tabela*)malloc(sizeof(tabela*));
    Surely you want sizeof(tablea) (or sizeof *tab) instead of tablea*. You're just allocating enough space for a pointer, not for a struct.
    Salem and stahta01 like this.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2

    Thanks very much

    Thank you cas for your reply, i'l gonna analise it considering your kind answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault...please help
    By liaa in forum C Programming
    Replies: 6
    Last Post: 03-21-2009, 09:45 AM
  2. Segmentation fault - IPC
    By kbfirebreather in forum C Programming
    Replies: 7
    Last Post: 02-01-2009, 02:17 PM
  3. yet another segmentation fault :)
    By yougene in forum C Programming
    Replies: 10
    Last Post: 01-14-2009, 02:15 AM
  4. Segmentation Fault
    By Strat79 in forum C Programming
    Replies: 9
    Last Post: 09-18-2008, 11:46 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21