Problem with socket binding - "system" call


 
Thread Tools Search this Thread
Top Forums Programming Problem with socket binding - "system" call
# 1  
Old 05-09-2009
Question Problem with socket binding - "system" call

Hi,

I am having an issue with using sockets.

I have a program which binds to a socket and listen on it. Later I spawn a thread to handle some function. In the new thread created I need to call a shell script which executes the specified function. Here I am using a system command to call the shell script.

In the new thread I created I am not using the socket for either reading/writing.

1. First thing I am not sure why the shell script that i am using is binding to the listen socket.Smilie
following is the extract from lsof

socket1 15920 janardha 3u IPv4 60551055 TCP *:4500 (LISTEN)
runtest 15922 janardha 3u IPv4 60551055 TCP *:4500 (LISTEN)

socket1 is my main thread where I am binding to port 4500. In the new thread created I am calling a shell script "runtest" which is also binding to the port 4500.

2. Is there a way that the new thread created does not bind to port 4500?

The problem became more worse when..

For some reason the shell script is hung for long time. In the mean time the main process is restarted due to a core in another thread.
Now when the process comes up it finds the socket is already in use and fails to bind to the socket.

The bind failure keeps happening until the shell script is killed or the script exits on its own.
[I have found a solution to hung thread issue, where in the shell script, I spawn a shell, sleep for sometime and kill the main thread if it still exits. If the main thread is not hung I will kill the new spawned shell (this piece of code is not in the example I have mentioned)]

Now I am looking a way to avoid the socket to be used by the newly spawned thread. I mean when the shell script is running it should not be bound to the listen socket that is created in the main thread.


Please help me resolve this issue.

Your help is appreciated.

Thanks in advance.

Here I am attaching my example program.

----------------------------------------------------------------
Code:
#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<stdlib.h>

#define SA struct sockaddr
#define LISTENQ 15

#define MAXLINE 1000

void * start_func(void *val);

int main(int argc, char **argv)
{
  int listenfd, connfd;
  struct sockaddr_in servaddr;
  char buff[MAXLINE];
  time_t ticks;
  pthread_t thr_id;
  int val=1;

  listenfd=socket(AF_INET,SOCK_STREAM,0);

  memset(&servaddr,0,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  servaddr.sin_port=htons(4500);

  setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (int*)&val, sizeof(val));

  if (bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) != 0) {
    printf ("Bind failed , errno %d\n", errno);
    return 0;
  }

  listen(listenfd, LISTENQ);

  if (pthread_create(&thr_id, NULL, start_func, NULL) != 0){
    printf("failed to create a thread\n");
    return 0;
  }

  pthread_detach(thr_id);

  /* For testing I am putting main thread in sleep. */
  printf("Main thread in sleep for 300 secs\n");
  sleep(300);

}

/* thread function */
void *start_func(void *val) {

  int retVal=0;

  char *cmd="/home/janardha/code/runtest;";

  printf("calling system function\n");

  retVal = system(cmd);

  if (retVal != 0) {
    printf ("failed to execute the command\n");
    return 0;
  }

}

Shell script runtest.
-------------------
Code:
#!/usr/bin/ksh

ls -lrt
print "In sleep"
sleep 100

----------------------------------------------------------------

The extracts from the output are:

bash-3.1$ ./socket1
Main thread in sleep for 300 secs
calling system function
total 12
-rwxrwxr-x 1 janardha janardha 53 May 7 06:49 runtest
-rw-rw-r-- 1 janardha janardha 1536 May 9 07:21 socket1.c
-rwxrwxr-x 1 janardha janardha 7096 May 9 07:22 socket1
In sleep


Output of lsof:
--------------
socket1 15920 janardha 3u IPv4 60551055 TCP *:4500 (LISTEN)
runtest 15922 janardha 3u IPv4 60551055 TCP *:4500 (LISTEN)


output of process list:
---------------------
janardha 15920 32001 0 07:24 pts/25 00:00:00 ./socket1
janardha 15922 15920 0 07:24 pts/25 00:00:00 /usr/bin/ksh /home/janardha/code/runtest



thanks,
Janardhan
# 2  
Old 05-09-2009
Just a guess, but: a thread isn't a "new" part of a program, but a part of the code that executes in parallel. A call to system() is, basically, a fork(), exec(), and wait(). And with fork() the complete context of the process is copied, including your socket.

One option that I can think of would be to do the fork() in the thread, clean up the environment, and then call system().
# 3  
Old 05-09-2009
Thanks for the reply.

How do I clean up the environment?
# 4  
Old 05-09-2009
In your case, it should be enough to just close that copy of the socket. But I can't guarantee that it won't also close the parents socket, so you should test that first.
# 5  
Old 05-09-2009
or call fcntl() with SET_FD and an argument of 1. This sets the close-on-exec flag to true for the socket. The parent's copy of the descriptor remains open.
# 6  
Old 05-10-2009
Thanks for the solution. It worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What does "force devmap reload" as in "multipath -r" means for my system and stability of my system?

Cannot present unpresented disks back again. On a test server tried this as a solution "multipath -r" and it worked. Too worried to try it in production before I know all the information. Any info would be appreciated! Also some links to the documentation on this specific issue could help a... (1 Reply)
Discussion started by: jsteppe
1 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

3. AIX

[Tip] Problem with rpm ("different operating system")

I have once experienced this problem without understanding what caused it but now learned thatn there is even a PMR dealing with it. Sometimes it happens that you encounter the following (rather cryptical) error message when trying to install an rpm-package: package <rpm_package_name> is for a... (1 Reply)
Discussion started by: bakunin
1 Replies

4. Shell Programming and Scripting

Problem using "system" command in perl

Hello!!! I'm trying to pass the output from bash command to perl variable in a perl script, and I used the "system" command to execute the bash statment and pass the result to perl string variable, in this perl script I used a variable $file that store data for using it as a regular expression.... (2 Replies)
Discussion started by: evolabo
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Programming

Adding custom ("Hello") system call: help

I'm trying to add a custom ("Hello world" :o) system call. In /usr/src/linux/hello/ I put simple hello.c ...#include "linux/linkage.h" // for linking a system call #include "linux/kernel.h" // for "printk" asmlinkage int sys_hello() { printk(KERN_ALERT "Hello!"); return 1; }... and in... (5 Replies)
Discussion started by: courteous
5 Replies

7. IP Networking

Configure a range of ports to "socket" system call

Hello ; This what i want to do : I know that in the system call #include <sys/socket.h> int bind(int socket, const struct sockaddr *address, socklen_t address_len); you can specify the local port for your socket, but im using a private library , and im sure that in that library... (0 Replies)
Discussion started by: trutoman
0 Replies

8. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

9. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

10. AIX

TCP/IP socket binding problem

I have what appears to be a unique socket problem, although admittedly my tcp/ip programming experience is relatively limited. I have a AIX server process using TCP/IP berkely sockets, and a Windows (C#) process. The windows process takes input from a user and sends a message to the Unix... (1 Reply)
Discussion started by: adiaconou
1 Replies
Login or Register to Ask a Question