Sponsored Content
Top Forums Programming a strange segment fault about ltp-posix test Post 302328465 by yanglei_fage on Wednesday 24th of June 2009 11:11:42 AM
Old 06-24-2009
a strange segment fault about ltp-posix test

Hi all

In the ltp-posix test,there is a case in open_posix_testsuite\conformance\interfaces\timer_gettime\speculative/6-1.c

Quote:
#include <time.h>
#include <stdio.h>
#include <errno.h>
//#include "posixtest.h"
#define BOGUSTID 9999
int main(int argc, char *argv[])
{
timer_t tid;
struct itimerspec its;
tid = (timer_t) BOGUSTID;

if (timer_gettime(tid, &its) == -1) {
if (EINVAL == errno) {
printf("fcn returned -1 and errno==EINVAL\n");
//return PTS_PASS;
} else {
printf("fcn returned -1 but errno!=EINVAL\n");
printf("Test FAILED\n");
//return PTS_FAIL;
}
}
printf("fcn did not return -1\n");
//return PTS_PASS;
}
I run the above code,it will has a segment fault, if I modify it to below,it works well
Quote:
#include <time.h>
#include <stdio.h>
#include <errno.h>
//#include "posixtest.h"
#define BOGUSTID 9999
int main(int argc, char *argv[])
{
timer_t tid;
struct itimerspec its;
int tval=BOGUSTID;
printf("%d\n",tval);
tid = (timer_t) &tval;
printf("%d\n",tid);
if (timer_gettime(tid, &its) == -1) {
if (EINVAL == errno) {
printf("fcn returned -1 and errno==EINVAL\n");
//return PTS_PASS;
} else {
printf("fcn returned -1 but errno!=EINVAL\n");
printf("Test FAILED\n");
//return PTS_FAIL;
}
}
printf("fcn did not return -1\n");
// return PTS_PASS;
}
Anybody can tell me why?
 

8 More Discussions You Might Find Interesting

1. Programming

Segment Fault

When run it, segment fault. What is wrong? #include <stdio.h> #include <stdlib.h> const int max =20; //**************************************************** // Input Matrix //**************************************************** void inMatrixAA(int *AA, int row, int col)... (9 Replies)
Discussion started by: zhshqzyc
9 Replies

2. Shell Programming and Scripting

Strange error message with regex test...

Hi all, I have a script where i need to check the format of a string. finally, i'm waiting a "process name" and 2 numbers separated by a "," string like : "this_is_a_string.txt,1,10 should be ok" string ok : "apache.exe,1,10" string ok : "mysqld,50,0" string not ok : "ap ache,1,10"... (4 Replies)
Discussion started by: fgilain
4 Replies

3. Shell Programming and Scripting

test command looks strange...

I just ran into this today, and don't know what to make of it... if test x$SERVER = x then echo $USAGE exit 1 fi I know what everything in there does, except for the 'x' jazz... (2 Replies)
Discussion started by: jjinno
2 Replies

4. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

5. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Programming

Segment-fault handling for pthreads

Hi I have struggling a week to fix a program , in the begining i got SIGBUS , but after many attempts still the program gets SIGSEGV segment fault , In bellow i post the seg fault log + source codes. would really appreciate if experts help me to fix this segment fault error. any advice is... (2 Replies)
Discussion started by: pooyair
2 Replies

7. Programming

Segment fault for C++ program when return vector

I am trying to reverse complement DNA sequence (string) with a short c++ code using boost library. Code was compiled without any warning/error, but ran into Segmentation fault. My guess is the function to return a vector, but not sure. #include <iostream> #include <fstream> #include <string>... (14 Replies)
Discussion started by: yifangt
14 Replies

8. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies
PTHREAD_SETNAME_NP(3)					     Linux Programmer's Manual					     PTHREAD_SETNAME_NP(3)

NAME
pthread_setname_np, pthread_getname_np - set/get the name of a thread SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <pthread.h> int pthread_setname_np(pthread_t thread, const char *name); int pthread_getname_np(pthread_t thread, char *name, size_t len); Compile and link with -pthread. DESCRIPTION
By default, all the threads created using pthread_create() inherit the program name. The pthread_setname_np() function can be used to set a unique name for a thread, which can be useful for debugging multithreaded applications. The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the terminating null byte (''). The thread argument specifies the thread whose name is to be changed; name specifies the new name. The pthread_getname_np() function can be used to retrieve the name of the thread. The thread argument specifies the thread whose name is to be retrieved. The buffer name is used to return the thread name; len specifies the number of bytes available in name. The buffer spec- ified by name should be at least 16 characters in length. The returned thread name in the output buffer will be null terminated. RETURN VALUE
On success, these functions return 0; on error, they return a nonzero error number. ERRORS
The pthread_setname_np() function can fail with the following error: ERANGE The length of the string specified pointed to by name exceeds the allowed limit. The pthread_getname_np() function can fail with the following error: ERANGE The buffer specified by name and len is too small to hold the thread name. If either of these functions fails to open /proc/self/task/[tid]/comm, then the call may fail with one of the errors described in open(2). VERSIONS
These functions first appeared in glibc in version 2.12. CONFORMING TO
These functions are nonstandard GNU extensions. NOTES
pthread_setname_np() internally writes to the thread-specific comm file under the /proc filesystem: /proc/self/task/[tid]/comm. pthread_getname_np() retrieves it from the same location. EXAMPLE
The program below demonstrates the use of pthread_setname_np() and pthread_getname_np(). The following shell session shows a sample run of the program: $ ./a.out Created a thread. Default name is: a.out The thread name after setting it is THREADFOO. ^Z # Suspend the program [1]+ Stopped ./a.out $ ps H -C a.out -o 'pid tid cmd comm' PID TID CMD COMMAND 5990 5990 ./a.out a.out 5990 5991 ./a.out THREADFOO $ cat /proc/5990/task/5990/comm a.out $ cat /proc/5990/task/5991/comm THREADFOO Program source #define _GNU_SOURCE #include <pthread.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> #define NAMELEN 16 #define errExitEN(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void * threadfunc(void *parm) { sleep(5); // allow main program to set the thread name return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc; char thread_name[NAMELEN]; rc = pthread_create(&thread, NULL, threadfunc, NULL); if (rc != 0) errExitEN(rc, "pthread_create"); rc = pthread_getname_np(thread, thread_name, NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("Created a thread. Default name is: %s ", thread_name); rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO"); if (rc != 0) errExitEN(rc, "pthread_setname_np"); sleep(2); rc = pthread_getname_np(thread, thread_name, (argc > 2) ? atoi(argv[1]) : NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("The thread name after setting it is %s. ", thread_name); rc = pthread_join(thread, NULL); if (rc != 0) errExitEN(rc, "pthread_join"); printf("Done "); exit(EXIT_SUCCESS); } SEE ALSO
prctl(2), pthread_create(3), pthreads(7) Linux 2014-05-28 PTHREAD_SETNAME_NP(3)
All times are GMT -4. The time now is 02:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy