Search Results

Search found 2 results on 1 pages for 'pkp'.

Page 1/1 | 1 

  • Can one thread open a socket and other thread close it?

    - by Pkp
    I have some kernel threads in Linux kernel, inside my KLM. I have a server thread, that listens to the channel, Once it sees there is an incoming connection, it creates an accept socket, accepts the connection and spawns a child thread. It also passes the accepted socket to the child kernel thread as the (void *) argument. The code is working fine. I had a design question. Suppose now the threads have to be terminated, main and the child threads, what would be the best way to close the accept socket. I can see two ways, 1] The main thread waits for all the child threads to exit, each of the child threads close the accept sockets while exiting, the last child thread passes a signal to the main thread for it to exit . Here even though the main thread was the one that created the accept socket, the child threads close that socket, and they do this before the main thread exits. So is this acceptable? Any problems you guys forsee here? 2] Second is the main thread closes all the accept sockets it created before it exits. But there may be a possibility(corner case) that the main thread gets an exception and will have to close, so if it closes the accept sockets before exiting, the child threads using that socket will be in danger. Hence i am using the first case i mentioned.Let me know what you guys think?

    Read the article

  • c permutation of a number

    - by Pkp
    I am writing a program for magic box. As the only way around it is brute force, I wrote a program to compute permutations of a given array using bells algorithm. I wrote in the lines similar to http://programminggeeks.com/c-code-for-permutation/. It does work for array of 3 and 4. It does not work for an arryay of 8 numbers (1,2,3,4,5,6,7,8). I see that the combination 1 2 3 4 5 6 7 8 gets repeated couple of times. Also there are other combinations that gets repeated. I see that certain combinations don't get displayed even. So could someone tell me what is wrong in the program below. Code: include<stdio.h> int len,numperm=1,count=0; display(int a[]){ int i; for(i=0;i<len;i++) printf("%d ",a[i]); printf("\n"); count++; } swap(int *a,int *b){ int temp; temp=*a; *a=*b; *b=temp; } no_of_perm(){ int x; for(x=1;x<=len;x++) numperm=numperm*x; } perm(int a[]){ int x,y; while(count < numperm){ for(x=0;x<len-1;x++){ swap(&a[x],&a[x+1]); display(a); } swap(&a[0],&a[1]); display(a); for(y=len-1;y>0;y--){ swap(&a[y],&a[y-1]); display(a); } swap(&a[len-1],&a[len-2]); display(a); } } main(int argc, char *argv[]){ if(argc<2){ printf("Error\n"); exit(0); } int i,*a=malloc(sizeof(int)*atoi(argv[1])); len=atoi(argv[1]); for(i=0;i<len;i++) a[i]=i+1; no_of_perm(); perm(a); }

    Read the article

1