Reputation: 11
I'm working with processes and threads on C language via Netbeans (with Windows 7). I'm using command line arguments but when it cames to run the program there's no way it will work. If I use the Run Netbeans button it won't ask for the arguments I need to enter and will display the message:
/cygdrive/C/Program Files/Netbeans 8.1/ide/bin/nativeexecution/dorun.sh: line 33: 3592 Segmentation Fault (core dumped) sh "${SHFILE}"
I'm trying to use the cmd console but it seems like I'm making something wrong calling the function this way:
gcc ej1.c 2
I'm supposed to use this format:
gcc font_file.c -o exe_file.exe
but there's no .exe file on the Netbeans folder as far as I know. Here's the message I get when running through Windows cmd.
And here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char *argv[]) {
HANDLE hThread2;
DWORD IDThread2;
int n = atoi(argv[1]);
printf("Parámetro: n = %d\n\n",n);
printf("Soy el proceso %d\n",(int)GetCurrentProcessId());
printf("Comienza el hilo primario (ID %d)\n\n",(int)GetCurrentThreadId());
void func(int *n){
printf("Comienza el hilo secundario (ID %d)\n",(int)GetCurrentThreadId());
int i;
int var = 0;
for(i=0; i<*n; i++){
var++;
}
printf("Valor final de la variable: %d\n",var);
printf("Finaliza el hilo secundario (ID %d)\n\n",(int)GetCurrentThreadId());
}
hThread2 = CreateThread (NULL,
0,
(LPTHREAD_START_ROUTINE) func,
&n,
0,
&IDThread2);
WaitForSingleObject(hThread2, // Este es el descriptor dell objeto por el que se espera
INFINITE);
CloseHandle(hThread2);
printf("Finaliza el hilo primario (ID %d)\n",(int)GetCurrentThreadId());
return 0;
}
Upvotes: 1
Views: 93
Reputation: 25753
Function CreateThread
requires the third parameter to be of type ThreadProc
, which is a function pointer of type DWORD(*)(LPVOID)
.
DWORD
is an unsigned 32 bit integer and LPVOID
is a pointer to void.
The function your code passes to CreateThread
has the type void(*)(int*)
, the types are clearly incompatible.
C Standard states that calling a function through a function pointer that is not compatible with the function type, will result in undefined behavior. This could manifest itself as a segmentation fault.
Upvotes: 1