c - Whats is purpose ftok in Message queues -
i have started reading message queues 1 of ipc mechanism on linux .but @ first step have basic questions.
use of
ftok()
generate unique id (key) , unique id generated.can't use simple number our keys rather using
ftok()
?what purpose of argument
key
inmsget
function?#include "sys/msg.h" key = ftok("/home/beej/somefile", 'b'); msqid = msgget(key, 0666 | ipc_creat);
what difference between
msqid
,key
?
the ftok
function creates sort of identifier used system v ipc functions (semget
, shmget
, msgget
). think of filedescriptor: when open file, pass path open
, number in return used read
, write
identify file. ftok
function serves similar purpose, while filedescriptor's scope limited process called open
(and children), ftok
token valid across system.
the reason system scope want 2 or more independent processes have access same ipc resources. if have 2 programs, both of execute key = ftok("/home/beej/somefile", 'b');
, both same token , can therefor access same resources (semaphores, shared memory, message queues). that's whole point of inter process communication.
you cannot use "simple number" don't know whether token might example index system-internal table or something. in other words, don't know how token used internally need use ftok
.
the man page says: "the specified path must specify existing file accessible calling process or call fail. also, note links files return same key, given same id." assume @ least ftok
implementations create token looking inode number of file specified path , combine second argument create token. second argument exists can create bunch of ipc resources (like several semaphores protect different resources).
as difference of key_t
(the value returned ftok
) , value retured msgget
: former gives access bunch of ipc resources (semaphore, shared memory , message queue), while later identifies specific message queue.
Comments
Post a Comment