Understanding Race Status

I would like to know if my program can make a racing condition or not? If yes, please give me an example because I do not see anyone.

#define STRING_SIZE 1024
char *string; 
int main(int argc,char**argv){
 int length; 
 if(argc != 2) return ;
 length = strlen(string);
 strncpy(string+length,argv[1],STRING_SIZE,STRING_SIZE-length);
return 0;

}

What if I do a lock? does this fix the problem?

#define STRING_SIZE 1024
int lock;
char *string; 
int main(int argc,char**argv){
 int length; 
 while(lock != 0){}
 lock = 1;
 if(argc != 2) return ;
 length = strlen(string);
 strncpy(string+length,argv[1],STRING_SIZE,STRING_SIZE-length);
 lock = 0;
 return 0;
}
+4
source share
1 answer

Race conditions occur if there is more than one thread of execution in a process, and all of them tend to (have access) to work (read / change) a common variable.

In your case there is only a thread executed - mainalthough char *stringglobal.Hence is not declared there is no race condition.

, pthread. / ( `char * ). . .

. char *string . strlen .

+3

All Articles