Handler callback is not calling properly in Signals for SIGCHLD - linux

I registered handler for SIGCHLD stop/exit and i am creating 4 child i am modifying global variable in it works fine as copy on write(COW). But handler function is not called for 4 child. when i print the global variable in parent, its value is 18 as i am expecting 21.
[Attached the output of image]
[1]: https://i.stack.imgur.com/7QOze.png
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#define N 4
int val = 9;
void handler(int sig)
{
val += 3;
printf("Inside handler getpid() = %d val = %d\n", getpid(), val);
return;
}
int main()
{
pid_t pid;
int i;
signal(SIGCHLD, handler);
for (i = 0; i < N; i++)
{
if ((pid = fork()) == 0)
{
val -= 3;
printf("Inside child getpid() = %d val = %d\n", getpid(), val);
exit(0);
}
}
for (i = 0; i < N; i++)
{
waitpid(-1, NULL, 0);
//printf("Inside parent getpid() = %d val = %d\n", getpid(), val);
}
printf("val = %d\n", val);
}

Related

Linux pipes and select

There is a shell script
#!/bin/bash
while :
do
echo "++stdout..";
echo "++stderr.." >> /dev/stderr;
sleep 1
done
and c prog
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
pid_t pid;
int pipefd[2];
int pipe_stderr[2];
char parametr[32];
if(argc != 2)
{
fprintf(stderr,"Niepoprawna liczba argumetnow procesu P \n");
exit(EXIT_FAILURE);
}
/* pipe
pipe0 - write
pipe1 - read
*/
if(pipe(pipefd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
if(pipe(pipe_stderr) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
sprintf(parametr,"%d",pipefd[0]); //convert - we want to use exec
pid = fork() ;
if(pid < 0)
{
perror("fork!");
}
else if(pid == 0)
{
puts("child");
dup2(pipefd[1],STDOUT_FILENO); // write
dup2(pipe_stderr[1],STDERR_FILENO); // write
close(pipefd[0]);
close(pipe_stderr[0]);
execv(argv[1],&argv[1]);
}
else{
puts("parent");
fd_set set;
struct timeval timeout;
int sel;
char buf;
close(pipefd[1]) ;
close(pipe_stderr[1]) ;
fcntl(pipefd[0],F_SETFL,O_NONBLOCK);
fcntl(pipe_stderr[0],F_SETFL,O_NONBLOCK);
FD_ZERO(&set);
FD_SET(pipefd[0], &set); // READ
FD_SET(pipe_stderr[0], &set); // READ
timeout.tv_sec = 17;
timeout.tv_usec = 0;
while(1){
puts("Select");
sel = select(FD_SETSIZE,&set,NULL,NULL,&timeout);
if(sel < 0 )
{
perror("Select");
}
else if(sel == 0)
{
printf("No communicate \n");
}
else{
if(FD_ISSET(pipefd[0], &set)){
puts("PARENT_READ_STDOUT:");
while(1){
ssize_t ret = read(pipefd[0], &buf,1);
if(ret > 0){
write(STDOUT_FILENO, &buf,1);
}else{
break;
}
}
}
if(FD_ISSET(pipe_stderr[0], &set)){
puts("PARENT_READ_STDERR:");
while(1){
ssize_t ret = read(pipe_stderr[0], &buf,1);
if(ret > 0){
write(STDERR_FILENO, &buf,1);
}else{
break;
}
}
}
}
}
}
}
the output of
./main ./test.sh
is
parent
Select
child
PARENT_READ_STDOUT:
++stdout..
Select
PARENT_READ_STDOUT:
++stdout..
Select
PARENT_READ_STDOUT:
++stdout..
Select
PARENT_READ_STDOUT:
++stdout..
Select
PARENT_READ_STDOUT:
++stdout..
Select
PARENT_READ_STDOUT:
++stdout..
Select
where did the stderr output go??
timeout.tv_sec = 17;
timeout.tv_usec = 0;
while(1){
FD_ZERO(&set);
FD_SET(pipefd[0], &set); // READ
FD_SET(pipe_stderr[0], &set); // READ

QProcess outputs with delay threads' stdout/stderr of multithreaded application

My platform:
Ubuntu 17.10 32-bit (Vbox VM)
Qt Creator 3.5.1 (opensource)
Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 32 bit
I am trying to invoke a multithreaded program (with arguments) using QProcess.start().
My program runs fine on terminal, i.e. periodically prints on stdout.
Using a TextEdit to log the stdout/stderr of the program I have connected QProcess readyReadStandardOutput/Error signals.
The stdout/stderr that comes from the main thread of the program is correctly shown on the TextEdit, the rest of the output (the one from all the other threads) is not shown.
EDIT
On the main thread an HTTP server is listening.
If a HTTP request is performed by browser at the url "127.0.0.1:32001" (port 32001 is hard coded in the QT code), when the HTTP request is received the program appends the HTTP packet and all the pending output from the other thread (moduleThread) to the TextEdit, so it could be a problem of flushing.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "http_server.h"
static pthread_t moduleThr;
static pthread_attr_t moduleThread_attr;
static bool one_second_elapsed;
static sem_t oneSecondSem;
void *moduleThread(void *arg0)
{
bool one_second_elapsed_local;
while (1)
{
sem_wait(&oneSecondSem);
one_second_elapsed_local = one_second_elapsed;
one_second_elapsed = false;
sem_post(&oneSecondSem);
if (one_second_elapsed_local)
fprintf(stdout, "Hello world each second!\r\n");
usleep(50000);
}
}
static void oneSecElapsed(int signum)
{
sem_wait(&oneSecondSem);
one_second_elapsed = true;
sem_post(&oneSecondSem);
}
static void TIMER_1sec_init(void)
{
struct sigaction sa;
struct itimerval timer;
/* Install timer_handler as the signal handler for SIGALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &oneSecElapsed;
sigaction (SIGALRM, &sa, NULL);
/* Configure the timer to expire after 1 sec... */
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
/* ... and every 1 sec after that... */
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 0;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_REAL, &timer, NULL);
}
/*
* ======== main ========
*/
int main(int argc, char *argv[])
{
const char *tcpport;
if (argc > 2)
{
fprintf(stdout, "id = %s\r\n", argv[1]);
fprintf(stdout, "tcpport = %s\r\n", argv[2]);
tcpport = argv[2];
}
else
exit(-1);
sem_init(&oneSecondSem, 0, 1);
one_second_elapsed = false;
/* Create thread quadro manager */
pthread_attr_init(&moduleThread_attr);
pthread_attr_setstacksize(&moduleThread_attr, 2048);
pthread_create(&moduleThr, &moduleThread_attr, moduleThread, NULL);
TIMER_1sec_init();
HTTPSERVER_init(tcpport);
/* should never return */
return (0);
}
http_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* memset() */
#include <stdbool.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include "module.h"
typedef struct { char *name, *value; } header_t;
static header_t reqhdr[17] = { {"\0", "\0"} };
//#define PORT "32001" /* Port to listen on */
#define BACKLOG 10 /* Passed to listen() */
static char *buf;
static char *method; // "GET" or "POST"
static char *uri; // "/index.html" things before '?'
static char *qs; // "a=1&b=2" things after '?'
static char *prot; // "HTTP/1.1"
static char *payload; // for POST
static int payload_size;
// get request header
char *request_header(const char* name)
{
header_t *h = reqhdr;
while(h->name) {
if (strcmp(h->name, name) == 0) return h->value;
h++;
}
return NULL;
}
void handle(int newsock)
{
//static int count = 0;
/* recv(), send(), close() */
int rcvd;
buf = malloc(65535);
rcvd=recv(newsock, buf, 65535, 0);
if (rcvd<0) // receive error
fprintf(stderr,("recv() error\n"));
else if (rcvd==0) // receive socket closed
fprintf(stderr,"Client disconnected unexpectedly.\n");
else // message received
{
buf[rcvd] = '\0';
//fputs(buf, stdout);
//fprintf(stdout, "count = %d\n", count);
method = strtok(buf, " \t\r\n");
uri = strtok(NULL, " \t");
prot = strtok(NULL, " \t\r\n");
fprintf(stderr, "\x1b[32m + [%s] %s\x1b[0m\n", method, uri);
if ((qs = strchr(uri, '?')))
{
*qs++ = '\0'; //split URI
} else {
qs = uri - 1; //use an empty string
}
header_t *h = reqhdr;
char *t = (char *)reqhdr;
char *t2;
while(h < reqhdr+16) {
char *k,*v;
k = strtok(NULL, "\r\n: \t");
if (!k)
break;
v = strtok(NULL, "\r\n");
while(*v && *v==' ')
v++;
h->name = k;
h->value = v;
h++;
fprintf(stderr, "[H] %s: %s\n", k, v);
t = v + 1 + strlen(v);
if (t[1] == '\r' && t[2] == '\n')
break;
}
t+=2;
t++; // now the *t shall be the beginning of user payload
t2 = request_header("content-length"); // and the related header if there is
payload = t;
payload_size = t2 ? atol(t2) : (rcvd-(t-buf));
fprintf(stdout, "-- payload len = %d >", payload_size);
fputs(payload, stdout);
fprintf(stdout, "<\r\n");
if (strcmp(method, "GET") == 0)
{
fprintf(stdout, "\nit's a GET!\r\n");
}
else if (strcmp(method, "POST") == 0)
{
fprintf(stdout, "\nit's a POST!\r\n");
}
sprintf(buf, "HTTP/1.1 200 OK\r\n\r\n");
send(newsock, buf, strlen(buf), 0);
close(newsock);
free(buf);
}
}
int HTTPSERVER_init(const char *tcpport)
{
int sock;
struct addrinfo hints, *res;
int reuseaddr = 1; /* True */
sigset_t sigset, oldset;
sigfillset(&sigset);
pthread_sigmask(SIG_BLOCK, &sigset, &oldset);
/* Get the address info */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(NULL, tcpport, &hints, &res) != 0) {
perror("getaddrinfo");
return 1;
}
/* Create the socket */
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == -1) {
perror("socket");
return 1;
}
/* Enable the socket to reuse the address */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
perror("setsockopt");
return 1;
}
/* Bind to the address */
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
perror("bind");
return 1;
}
/* Listen */
if (listen(sock, BACKLOG) == -1) {
perror("listen");
return 1;
}
freeaddrinfo(res);
/* Main loop */
while (1) {
printf("waiting on accept\n");
fflush(stdout);
socklen_t size = sizeof(struct sockaddr_in);
struct sockaddr_in their_addr;
int newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
if (newsock == -1) {
perror("accept");
}
else {
printf("Got a connection from %s on port %d\n",
inet_ntoa(their_addr.sin_addr), htons(their_addr.sin_port));
handle(newsock);
}
}
close(sock);
return 0;
}
As pointed out by #eyllanesc, the pending strings "Hello world each second!\r\n" come out eventually after several seconds.
The moduleThread shoud be a thread and not a different process: this is verified by commadn "ps aux" that shows only "../program/testApp test 32001".
Could any of you kindly give me a hint on what is going on here please?
Thanks for your time,
Francesco

Alternate Without Semaphores

The program below creates two processes.
The parent process prints the numbers from 0 to 9, while the
child process prints the characters from A to J.
How can I make it alternate the printing using only signals? so no using semaphores or any IPCs.
#include <stdio.h>
int main()
{
int i = 0;
if (fork() == 0) /* Child process */
{
for (i = 0; i < 10; i++) {
printf("%c\n", i + 65);
}
} else /* Parent process */
{
for (i = 0; i < 10; i++) {
printf("%d\n", i);
}
}
}
I figured it out.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
int main()
{
int i=0;
int status;
int childPID = fork();
if (childPID == 0) /* Child process */
{
for(i=0;i<10;i++){
printf("%c\n",i+65);
kill(getpid(), SIGSTOP);
}
} else /* Parent process */
{
for(i=0;i<10;i++) {
waitpid(childPID,&status,WUNTRACED);
printf("%d\n",i);
kill(childPID, SIGCONT);
}
}
}

select function may upate timeout parameter in linux.why it is 'may update'?

My operating system is Centos 6.4.
For select the linux programmer's manual says: "select may update the timeout parameter to indicate how much time was left...."
I wonder why it is 'may' rather than 'must'? Is there a system version or a kernel version?
I make a test.
test_code01.cpp
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
struct timeval g_timeout;
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
FD_SET(sock_clientfd, &readfds);
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
// Becasue select can update timeout, we need to set value for timeout.
// g_timeout.tv_sec = 3;
// g_timeout.tv_usec = 0;
// Recover sock_clienfd state
FD_SET(sock_clientfd, &readfds);
if(ret_select == 0)
{
printf("select wait timeout.\n");
continue;
}
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
I execute this test_code01.cpp, and the result select wait for 3 seconds for only first times.In my opinion, the select modify timeout.So I modify code follow:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
// in order to facilitate the ovservation data
// the timeout is set to global varible
struct timeval g_timeout;
void* thredproc_looktimeout(void*)
{
while(1)
{
printf("left time: %ds %dms\n", g_timeout.tv_sec, g_timeout.tv_usec);
}
}
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
FD_SET(sock_clientfd, &readfds);
printf("fdset add fd start+++\n");
for(int i = 0; i < 2000; i++)
{
FD_SET(1025+i, &readfds);
}
printf("%d--%s\n", errno, strerror(errno));
printf("fdset add 2000 fd finish...\n");
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
/**
* look timeout left time by create thread
**/
pthread_t tid;
int ret_pthreadcreate = pthread_create(&tid, NULL, thredproc_looktimeout, NULL);
printf("pthread_create return value is %d\n", ret_pthreadcreate);
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
// Recover sock_clienfd state
FD_SET(sock_clientfd, &readfds);
if(ret_select == 0)
{
printf("select wait timeout.\n");
continue;
}
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
I think we should see a change in timeout in thredproc_looktimeout.But no change.I don't know why?
The manual page means that on return the timeout might be updated to indicate the amount of time left. It absolutely doesn't mean that the timeout will be updated while the wait is still in progress.
And even if it was, there is no possible way you could detect it. Your attempt to do with threads won't work because it has no synchronization of any kind. Among other problems, the compiler could optimize the while loop to read g_timeout only once. And there is no conceivable synchronization mechanism you could use.
Because a valid timeout may also be
null in the case you want to wait forever
both fields of the timeval are set to 0, select returns immediately
In both those cases subtraction doesn't make sense, hence no "must."
Your addition of code makes this a duplicate of https://stackoverflow.com/a/7382740/4080377. You're seeing a copy of the variable, not the real thing
I get a answer.In my system, select actualy modify timeout value.I don't fully understand select may update timeout before.
Now,
(1)all_time:it is initial value of timeout.For example, timeout.tv_sec is equal to three and timeout.tv_usec is equal to zero.
(2)consume_time:it is the time value that from start to return of select to spended.
(3)left_time:it does't use time for select.
Linux manual actually implies the meaning that left_time add consume_time is equal to all_time.
Why it "may update".I think Tom Scanlan's answer is right.
The follow code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
// in order to facilitate the ovservation data
// the timeout is set to global varible
struct timeval g_timeout;
void* thredproc_looktimeout(void*)
{
while(1)
{
if(g_timeout.tv_sec != 3 && g_timeout.tv_usec != 0)
{
printf("left time: %ds %dms\n", g_timeout.tv_sec, g_timeout.tv_usec);
}
}
}
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
FD_SET(sock_clientfd, &readfds);
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
/**
* look timeout left time by create thread
**/
pthread_t tid;
int ret_pthreadcreate = pthread_create(&tid, NULL, thredproc_looktimeout, NULL);
printf("pthread_create return value is %d\n", ret_pthreadcreate);
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
// Recover sock_clienfd state
FD_SET(sock_clientfd, &readfds);
if(ret_select == 0)
{
printf("select wait timeout.\n");
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
continue;
}
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}
sleep(3);
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
This code can get no-zero value in thredproc_looktimeout thread function.
In addition, such is in case, As David Schwartz said: it absolutely doesn't mean that the timeout will be updated while the wait is still in progress.
You can try this code. I added some code and some comment.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
// in order to facilitate the ovservation data
// the timeout is set to global varible
struct timeval g_timeout;
void* thredproc_looktimeout(void*)
{
while(1)
{
if(g_timeout.tv_sec != 3 && g_timeout.tv_usec != 0)
{
printf("left time: %ds %dms\n", g_timeout.tv_sec, g_timeout.tv_usec);
}
}
}
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
int ret_select = 0;
fd_set readfds;
/*new code added*/
FD_ZERO(&readfs);
/* before you set any fd in readfs, you should set 0 all fds in reads. For security, because some times
readfs can alloced some old trash data place, and a chance in readfs may some data.*/
FD_SET(sock_clientfd, &readfds);
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
/**
* look timeout left time by create thread
**/
pthread_t tid;
int ret_pthreadcreate = pthread_create(&tid, NULL, thredproc_looktimeout, NULL);
printf("pthread_create return value is %d\n", ret_pthreadcreate);
while(1)
{
ret_select = select(sock_clientfd + 1, &readfds, NULL, NULL, &g_timeout);
if(ret_select == -1){
/*return some error or what you want here. Because if ret_select is equal to 1. That is a problem*/
}
// Recover sock_clienfd state
/* FD_SET(sock_clientfd, &readfds);*/
/* you don't need to same fd set again. That shuold be always some data ready to read()*/
if(ret_select == 0)
{
printf("select wait timeout.\n");
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
continue;
}
/*bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
if(ret_recvsize > 0)
{
printf("get %d message:%s", strlen(buffer), buffer);
ret_recvsize=0;
}
else
{
printf("no data from server\n");
}*/
/*Here you can check just sock_clienfd. If that is true, some data ready to read on socket.*/
if(FD_ISSET(sock_clientfd,&readfs){
bzero(buffer, BUFSIZE + 1);
ret_recvsize = recv(sock_clientfd, buffer, BUFSIZE, 0);
}
/*sleep(3);*/ /*your select() function already wait 3 second before go down*/
// Becasue select can update timeout, we need to set value for timeout.
g_timeout.tv_sec = 3;
g_timeout.tv_usec = 0;
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}

Linux - Syscall to Iterate over children threads

I am creating a syscall to return some information about a process and its children threads. I am testing this in Ubuntu 14.04. Here is the code that I have written:
#include <linux/list.h>
#include <linux/linkage.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#define NUM_THREADS 5
struct thread_info_prj {
int pid;
int nthreads;
int tid[NUM_THREADS];
};
asmlinkage void sys_threadinfo_prj(void *ptr)
{
struct task_struct *task;
struct thread_info_prj t_info;
struct list_head *list;
int num_threads = 0;
t_info.pid = current->pid;
list_for_each(list, &current->children) {
//task = list_entry(&p_task->children, struct task_struct, sibling);
task = list_entry(list, struct task_struct, sibling);
t_info.tid[num_threads] = task->pid;
num_threads++;
}
t_info.nthreads = num_threads;
copy_to_user(ptr, &t_info, sizeof(struct thread_info_prj));
}
This code is built into the kernel as a syscall , and tested with the following program.
#include <pthread.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <errno.h>
#define NUM_THREADS 5
struct thread_info_prj {
int pid;
int nthreads;
int tid[NUM_THREADS];
};
void * thread_fn(void *ptr) {
printf("Im a thread!\n");
sleep(4);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
int i;
struct thread_info_prj t_info;
void *status;
pthread_attr_init(&attr);
//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i = 0; i < NUM_THREADS; i++)
pthread_create(&(threads[i]), &attr, thread_fn, NULL);
sleep(1);
syscall(352, &t_info);
//for (i = 0; i < NUM_THREADS; i++)
// pthread_join(threads[i], &status);
printf("PID: %d\n", t_info.pid);
printf("Num Threads: %d\n", t_info.nthreads);
for (i = 0; i < t_info.nthreads; i++) {
printf("Thread ID: %d\n", t_info.tid[i]);
}
printf("%s\n",strerror(errno));
return 0;
}
Unfortunately, the only output is the proper parent pid. Otherwise, it returns that there are no threads and does not print any additional PIDs. By adding a printk inside the for loop, I discovered that it never even enters the for loop. Any suggestions?
Thanks so much.

Resources