From 15472c449d9da05fc1a4277d82c6c2126ae4b81d Mon Sep 17 00:00:00 2001 From: Raghavendra Bhat Date: Fri, 13 Jan 2012 17:41:17 +0530 Subject: ping_pong: allocate the memory from heap instead of using the stack address Currently the stack address of the structure which holds information about the number of locks, fd etc is passed to the thread doing the locking. Instead of sending the stack address, allocate memory from the heap and use that address to send to the other thread. Change-Id: I417e22aee0eed1fa921982e78239147553886786 Signed-off-by: Raghavendra Bhat --- c_pgms/ping_pong.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/c_pgms/ping_pong.c b/c_pgms/ping_pong.c index 7881266..90141cf 100644 --- a/c_pgms/ping_pong.c +++ b/c_pgms/ping_pong.c @@ -193,7 +193,7 @@ int main(int argc, char *argv[]) char *fname; int fd, num_locks; int c; - file_info_t info_file; + file_info_t *info_file = NULL; pthread_t thread; int tid = -1; int zzzz = 600; @@ -247,10 +247,16 @@ int main(int argc, char *argv[]) fd = open(fname, O_CREAT|O_RDWR, 0600); if (fd == -1) exit(1); - info_file.fd = fd; - info_file.num_locks = num_locks; + info_file = calloc (1, sizeof (*info_file)); + if (!info_file) { + ret = -1; + goto out; + } + + info_file->fd = fd; + info_file->num_locks = num_locks; - tid = pthread_create (&thread, NULL, (void *)ping_pong, (void *)&info_file); + tid = pthread_create (&thread, NULL, (void *)ping_pong, (void *)info_file); if (zzzz == 0) { printf ("running indefinitely\n"); -- cgit