-1

I am pretty confuse why my program is not working as intended.

I have three similar function:

static void *L1(void *ptr )
{
    char *text = (char *)ptr;
    int ret;
    int fd = open("/dev/L1", O_RDWR); 

    ret = write(fd, text, strlen(Send)); // Send string to LKM
}

static void *L2(void *ptr )
{
    char *text = (char *)ptr;
    int ret;
    int fd = open("/dev/L2", O_RDWR); 

    ret = write(fd, text, strlen(Send)); // Send string to LKM
}

static void *L3(void *ptr )
{
    char *text = (char *)ptr;
    int ret;
    int fd = open("/dev/L3", O_RDWR); 

    ret = write(fd, text, strlen(Send)); // Send string to LKM
}

Next I have a function for creating thread:

static void createKThread(void* (*a)(void *), void* (*b)(void *), void* (*c)(void *))

{
  pthread_t t1, t2, t3;
  int T1, T2, T3;

  T1 =  pthread_create(&t1, NULL, a, NULL);
  T2 =  pthread_create(&t2, NULL, b, NULL);
  T3 =  pthread_create(&td3, NULL, c, NULL);

  pthread_join(t1, NULL);
  pthread_join(t2, NULL);
  pthread_join(t3, NULL);

}

In my main I am doing :

     int main(){

     Send2[0] = 'a';
     createKThread(L1(&Send[0]), L2(&Send[0]), L3(&Send[0])); //First

     Send2[0] = 'b';
     createKThread(L1(&Send[0]), L2(&Send[0]), L3(&Send[0])); //Second
     return 0;
     }

My main program terminates after completion of the first function call `createKThread´. I can't figure out why, I would like the second function call to work and I would like to add more code to my main program. Please advice me on what I am doing wrongly.

BDL
  • 21,052
  • 22
  • 49
  • 55
D.Bryan
  • 19
  • 6

2 Answers2

0

You mess up your thread functions. I assume your functions L1 etc. are supposed to run as thread functions.

static void *L1(void *ptr )
{
    char *text = (char *)ptr;
    int ret;
    int fd = open("/dev/L1", O_RDWR); 

    ret = write(fd, text, strlen(Send)); // Send string to LKM
}

Doesn't your compiler shout about missing return value?

When you call your create function, you pass the return values from these functions:

createKThread(L1(&Send[0]), L2(&Send[0]), L3(&Send[0])); //First

Instead of passing the address of the functions, you call them! As they return random value you create threads with random function addresses.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • Hi Gerhardh, the compiler does not give me any error at all. I been trying to find the problem for hours. Thanks for your advice. – D.Bryan Nov 14 '17 at 12:28
  • Then you should enable warnings. As you should always do! Missing return value for a non-void function and incorrect return type for `main` should be the minimum of warnings you should get. Also wrong parameter types for `createKThread` should show a warning – Gerhardh Nov 14 '17 at 12:36
0

Here:

 createKThread(L1(&Send[0]), L2(&Send[0]), L3(&Send[0])); //First

your argument L1(&Send[0]) is evaluated by calling L1 with parameter &Send[0]. This is not what you want. You want to pass the function pointer L1 and the argument separately, i.e.:

createKThread(L1, &Send[0], L2, &Send[0], L3, &Send[0]);

with

static void createKThread(void* (*a)(void *), void *aarg,
                          void* (*b)(void *), void *barg,
                          void* (*c)(void *), void *carg) {
    ...
    T1 =  pthread_create(&t1, NULL, a, aarg);
    ...
}

etc. Then it should work as you expect.

As a side note: You should pay attention to crash messages (i.e. "Segmentation fault") or compiler warnings (enable all!) when debugging a problem.

Ctx
  • 18,090
  • 24
  • 36
  • 51