Archive

Posts Tagged ‘data’

Shared variables between child processes in C [fork()]

March 5, 2014 No comments

Another way of facing concurrency in the wonderful world of doing several task at the same time is using child processes with fork(). The main difference between fork and threads is that forked process are full process, they have their own memory for code, for data and stack, while threads share code and data, and have their own stack.

But, what about sharing variables in forked processes? If we try to make it as simpler as in thread examples, it’s not going to work, because as they are different processes, they are using different memory zones for their data. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main()
{
  int child;
  int *number = malloc(sizeof(int));
  int i;

  *number = 10;

  child = fork();

  if (child==-1)
    {
      exit(1);
    }
  else if (child==0)
    {
      for (i=0; i<10; ++i)
    {
      usleep(100);
      printf ("CHILD -- Number: %d\n", *number);
      *number=i;
    }
      exit(0);
    }
  else
    {
      for (i=20; i<30; ++i)
    {
      usleep(100);
      printf ("MAIN -- Number: %d\n", *number);
      *number=i;
    }
    }
  wait(NULL);
  free(number);
}

In our ideal world, the MAIN process can see the values written by CHILD and vice versa, but the value the MAIN process can access is completely different than the value the CHILD can access, even using malloc before calling fork(). fork() copies the values of all variables before fork(), but the physical memory address is different.

To do that we have to work with shared memory, have to use mmap instead of malloc:

1
2
  int *number = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
               MAP_SHARED | MAP_ANONYMOUS, -1, 0);

And, like malloc() uses free() to free memory, with mmap we will use:

1
mmunmap(number, sizeof(int));

And, of course, when using threads we can have a race condition, we must use mutex here too to solve the problem.
Photo: Rudolf Vicek (Flickr) CC-by

Non-blocking TCP client in C, where we can send and receive data at every moment

January 10, 2014 No comments

If you are creating a program which interacts with a server, this may interest you. First, we’re going to make a simple TCP client, we connect to a server and it will allow us to send whatever we write on our keyboard. We can’t receive anything from the server right now, it’s just a first step to reach our goal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>

#define MENS_MAX_LEN 500

void usage()
{
  fprintf (stderr, "Wrong arguments. Must give two:\n");
  fprintf (stderr, "tcpclient SERVER PORT\n\n");
  fprintf (stderr, "For example:\n");
  fprintf (stderr, "  tcpclient totaki.com 80\n");
  exit(1);
}

void panic(char *msg)
{
  fprintf (stderr, "Fatal error: %s (errno %d, %s)\n", msg, errno, strerror(errno));
  exit(2);
}

int main(int argc, char *argv[])
{
  char *server;
  int port;
  int socketfd;
  int finish = 0;
  struct sockaddr_in serverAddress, clientAddress;
  struct hostent *h;
  char mens [MENS_MAX_LEN];
  size_t recvsize;

  if (argc<3)
    usage();

  server = argv[1];
  port = atoi(argv[2]);

  /* Create a TCP socket */
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd==-1)
    panic("Failed to create socket");

  /* Sets client address */
  clientAddress.sin_family = AF_INET;
  clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  clientAddress.sin_port = htons(0);

  /* Bind the address to the socket */
  if (bind(socketfd, (struct sockaddr *)&clientAddress, sizeof(clientAddress))==-1)
    panic("Binding address");

  /* Get the hostname address */
  h = gethostbyname(server);

  /* Sets server address */
  serverAddress.sin_family = h->h_addrtype;
  memcpy((char*) &serverAddress.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  serverAddress.sin_port = htons(port);

  if (connect(socketfd, (struct sockaddr * )&serverAddress, sizeof(serverAddress))==-1)
    panic("Cannot connect");

  do
    {
      fgets(mens, MENS_MAX_LEN, stdin);

      if (send(socketfd, mens, strlen(mens)+1, 0)==-1)
    panic ("Cannot sent message");

    } while (!finish);      /* Never finish */

  return EXIT_SUCCESS;
}

We’ll analyze the main() function directly, because usage() just display text and panic() makes the program exit on an error.
The first thing we do is to determine the server and port we will connect to, they both will be the first and second arguments to our program. Then we create the socket and build the client address. The port is 0 to let the system pick any port, we don’t mind which port is picked. Then bind() will assign the address to the socket.

Then, create the server address. We will use getbyhostname() before because we may resolve the server address first (if it comes with a hostname, or even if it comes as a IP address we will have to translate this address to a suitable format), so let’s connect() and begin sending messages.

In this case we’re using fgets() to ask the user for information through the standard input (usually keyboard) and then send() it, we must set the size to strlen(mens)+1 to include the string terminator ‘\0′ at the end of the string being sent.

Note: fgets() will send the line feed at the end when we press return. We can use a trim() function or filter it with:

1
2
if (mens[strlen(mens)-1]=='\n')
   mens[strlen(mens)-1] = '\0';

And to receive a message from the server, we must:

1
2
3
4
5
size_t recvsize;
recvsize = recv(socketfd, mens, MENS_MAX_LEN, 0);
if (recvsize==-1)
  panic("Cannot receive information");
mens[recvsize] = '\0';

Reception is similar to sending, but this time we don’t know the size of the message, so we provide a maximum size (or the size of our buffer), if the size of the data we are receiving is greater than our buffer, we can continue the message calling again and again recv().
As this message may not have a terminator, to display it properly on screen with printf() we must put the terminator manually at the end of the string.

It’s easy. The biggest problem, comes when we want the application to send and receive information whenever there is data on each side (sending or reception), making our application full duplex. With this code, right now, we just can give turns, make the user send information sometimes, and sometimes the application will receive data.
We will make a more complex example using select(), this function will detect whenever there is data waiting on some descriptors, and we can later choose whether to read the socket or the keyboard. But the most interesting is the timeout, we can give select() a wait timeout, when the function will stop waiting and the program can continue, it won’t block indefinitely waiting for data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>

#define MENS_MAX_LEN 500

void usage()
{
  fprintf (stderr, "Wrong arguments. Must give two:\n");
  fprintf (stderr, "tcpclient SERVER PORT\n\n");
  fprintf (stderr, "For example:\n");
  fprintf (stderr, "  tcpclient totaki.com 80\n");
  exit(1);
}

void panic(char *msg)
{
  fprintf (stderr, "Fatal error: %s (errno %d, %s)\n", msg, errno, strerror(errno));
  exit(2);
}

int main(int argc, char *argv[])
{
  char *server;
  int port;
  int socketfd;
  int finish = 0;
  struct sockaddr_in serverAddress, clientAddress;
  struct hostent *h;
  char mens [MENS_MAX_LEN];
  fd_set readmask;
  struct timeval timeout;
  size_t recvsize;

  if (argc<3)
    usage();

  server = argv[1];
  port = atoi(argv[2]);

  /* Create a TCP socket */
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd==-1)
    panic("Failed to create socket");

  /* Sets client address */
  clientAddress.sin_family = AF_INET;
  clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  clientAddress.sin_port = htons(0);

  /* Bind the address to the socket */
  if (bind(socketfd, (struct sockaddr *)&clientAddress, sizeof(clientAddress))==-1)
    panic("Binding address");

  /* Get the hostname address */
  h = gethostbyname(server);

  /* Sets server address */
  serverAddress.sin_family = h->h_addrtype;
  memcpy((char*) &serverAddress.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  serverAddress.sin_port = htons(port);

  if (connect(socketfd, (struct sockaddr * )&serverAddress, sizeof(serverAddress))==-1)
    panic("Cannot connect");

  do
    {
      /* We must set all this information on each select we do */
      FD_ZERO(&readmask);   /* empty readmask */
      /* Then we put all the descriptors we want to wait for in a */
      /* mask = readmask */
      FD_SET(socketfd, &readmask);
      FD_SET(STDIN_FILENO, &readmask); /* STDIN_FILENO = 0 (standard input) */
      /* Timeout, we will stop waiting for information */
      timeout.tv_sec=0;
      timeout.tv_usec=100000;

      /* The first parameter is the biggest descriptor+1. The first one
       was 0, so every other descriptor will be bigger.*/

      /* readfds = &readmask */
      /* writefds = we are not waiting for writefds */
      /* exceptfds = we are not waiting for exception fds */
      if (select(socketfd+1, &readmask, NULL, NULL, &timeout)==-1)
    panic("Error on SELECT");

      /* If something was received */
      if (FD_ISSET(socketfd, &readmask))
    {
      recvsize = recv(socketfd, mens, MENS_MAX_LEN, 0);
      if (recvsize==-1)
        panic("Cannot receive information");
      mens[recvsize] = '\0';
      printf (">> %s\n", mens);
    }

      /* If something was written by the user */
      if (FD_ISSET(STDIN_FILENO, &readmask))
    {
      fgets(mens, MENS_MAX_LEN, stdin);

      if (send(socketfd, mens, strlen(mens)+1, 0)==-1)
        panic ("Cannot sent message");
    }
    } while (!finish);

  return EXIT_SUCCESS;
}

Photo: Julien Gong Min (Flickr) CC-by

Logging to debug MySQL

October 30, 2013 No comments

It’s common, when using more or less complex stored procedures, to make mistakes that make us waste much time, and sometimes we would like to see the value of a variable or the result of a query because it could tell us what’s going wrong.

This is a little help, an easy piece of code we can insert into our database and will let is write in a table what’s happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
DROP SCHEMA IF EXISTS `PBUtils` ;
CREATE SCHEMA IF NOT EXISTS `PBUtils` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

USE PBUtils;

DROP TABLE IF EXISTS `PBUtils`.`LOG`;
CREATE TABLE IF NOT EXISTS `PBUtils`.`LOG` (
   `Log_id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
   `Log_date` DATETIME NOT NULL,
   `Log_description` varchar(128) NOT NULL,
   `Log_text` TEXT NULL,
   `Log_number` bigint NULL,
   `Log_float` double NULL,
   PRIMARY KEY(Log_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DELIMITER $$
DROP PROCEDURE IF EXISTS `PBUtils`.`logText` $$
CREATE PROCEDURE `PBUtils`.`logText`(
    IN  In_description VARCHAR(128),
    IN  In_text TEXT
)
BEGIN
    INSERT INTO PBUtils.LOG (`Log_date`, `Log_description`, `Log_text`) VALUES (NOW(), In_description, In_text);
END $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `PBUtils`.`logNumber` $$
CREATE PROCEDURE `PBUtils`.`logNumber`(
    IN  In_description VARCHAR(128),
    IN  In_number bigint
)
BEGIN
    INSERT INTO PBUtils.LOG (`Log_date`, `Log_description`, `Log_number`) VALUES (NOW(), In_description, In_number);
END $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `PBUtils`.`logFloat` $$
CREATE PROCEDURE `PBUtils`.`logFloat`(
    IN  In_description VARCHAR(128),
    IN  In_float double
)
BEGIN
    INSERT INTO PBUtils.LOG (`Log_date`, `Log_description`, `Log_float`) VALUES (NOW(), In_description, In_float);
END $$
DELIMITER ;

Now, what we have to do is insert our procedure to debug values, anywhere in our code:

1
PBUtils.logText('Text to describe', variable);
1
PBUtils.logNumber('Text to describe', variable);
1
PBUtils.logFloat('Text to describe', variable);

depending on what we want to record, a text, number or float

To see what has been written in our log:

1
SELECT * FROM PBUtils.LOG ORDER BY Log_data DESC

Variable number of arguments for a function in C

October 13, 2013 No comments

If you have ever program in C, I’m sure you had met printf(), it is, probably, the first thing you used in this language (at least for a Hello World). Well, this function accepts as many arguments as keywords you enter in its first argument, so here we have a variable number of arguments.

Some people think that only printf() and scanf() functions worth it, but there are so many cases where it is needed, for example when creating a function with optional arguments (these functions are common in C++, but C doesn’t allow it so easily), when concatenating some strings, filling data variables, internationalizing, and more.

All we must do is to include stdarg.h wichi provides us the tools to make it happen.

Let’s see this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

void putargs(int nargs, ...)
{
  va_list ap;
  int i;
  int num;

  va_start(ap, nargs); // nargs, because is the last known argument
  for (i =0; i<nargs; i++)
    {
      num=va_arg(ap, int); // All arguments are int
      printf("Argument %d = %d\n", i, num);
    }
  va_end(ap);
  printf("\n");
}

int main(int argc, char *argv[])
{
  putargs(5, 6, 77, 445, 34, 23, 12, 43, 32);
  putargs(2, 23, 94);
  putargs(0);

  return EXIT_SUCCESS;
}

We can see here, function putargs() prints out values of possible arguments we’ve passed the function, being the first of them the number of arguments we have, because we have no way to count the number of arguments passed (like printf, we will use as many arguments as %[x] we have in the format string). So we will have to use different techniques to know the total number of arguments, like:

  • Ask the user, like we’ve done here, with an argument telling the function how many arguments to read.
  • Search for a value inside all arguments, we can read arguments until the value I found is 5 (for example). It is used so often when passing pointers to the function, I will read arguments until I get a NUL value.
  • Like printf() use a number of keywords, this will be the number of arguments

We just have to copy this sample code and make some test, have in mind when we start looking for arguments, we must call va_start() with a va_list variable and the last known argument we gave the function, then each time we call va_arg, we will get a different argument from the “unknown argument list”, that is the list of arguments starting by the three dots.

In the next example we will have a variable argument function used to fill a struct with people data. We will have the function new_person where we must say the name, but it will add data to the struct as you are adding arguments when you call the function, it’s fun and easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#define MAX_STRLEN 64

struct TPersonData
{
  char name[MAX_STRLEN];
  char blog[MAX_STRLEN];
  char facebook[MAX_STRLEN];
  char twitter[MAX_STRLEN];
  char city[MAX_STRLEN];
  char drink[MAX_STRLEN];   /* Don't know anything else I can put for this example :) */
  char phone[MAX_STRLEN];
};

/* Remember always to clear data. Variable initialization is important if we don't
 want to see garbage. */

struct TPersonData cleanPerson()
{
  struct TPersonData p;
  strcpy(p.name, "");
  strcpy(p.blog, "");
  strcpy(p.facebook, "");
  strcpy(p.twitter, "");
  strcpy(p.city, "");
  strcpy(p.drink, "");
  strcpy(p.phone, "");

  return p;
}

struct TPersonData new_person (char *name, ...)
{
  va_list vl;
  char *temp;
  int num = 1;
  struct TPersonData out = cleanPerson();

  strcpy(out.name, name);

  va_start(vl, name);
 
  while ( (temp = va_arg(vl, char*))!=NULL)
    {
      switch (num++)
    {
    case 1:
      strcpy(out.blog, temp);
      break;
    case 2:
      strcpy(out.facebook, temp);
      break;
    case 3:
      strcpy(out.twitter, temp);
      break;
    case 4:
      strcpy(out.city, temp);
      break;
    case 5:
      strcpy(out.drink, temp);
      break;
    case 6:
      strcpy(out.phone, temp);
      break;
    default:
      break;
    }
    }
  va_end(vl);

  return out;
}

void printPerson(struct TPersonData data)
{
  printf ("Name: %s\n", data.name);
  printf ("Blog: %s\n", data.blog);
  printf ("Facebook: %s\n", data.facebook);
  printf ("Twitter: %s\n", data.twitter);
  printf ("City: %s\n", data.city);
  printf ("Drink: %s\n", data.drink);
  printf ("Phone: %s\n", data.phone);
  printf ("\n\n");
}

int main(int argc, char *argv[])
{
  struct TPersonData p = new_person("Gaspar Fernandez", "Binary Prose", NULL);
  printPerson(p);
 
  p = new_person("John Doe", "JD Blog", "http://facebook.com/johndoe", "@johndoe", "New York", NULL);
  printPerson(p);
  return EXIT_SUCCESS;
}

It’s important not to forget the NULL after all arguments are passed, or we will see some unexpected behavior in runtime.

Photo: Robyn Petrovich (Flickr) CC-by

Top