arg.val = 1;
if (sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT) == -1 ){
perror("Creating semaphore failed");
exit(1);
}
else {
printf("Creating a semaphore with ID: %d \n",sem_id);
if (semctl(sem_id, 0, SETVAL, arg) == -1 ) {
perror("Initialization of semaphore failed\n");
exit(1);
}
}
I am trying to create and initialize a semaphore and when I am compiling my program it returns me:
"Initialization of semaphore failed
:Identifier removed
Could you explain me the reason why this happens??
if (sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT) == -1 )
is parsed (because == binds stronger than =)
if (sem_id = (semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT) == -1) )
you probably want
if ((sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT)) == -1 )
The operation == has a higher precedence than the operation =. I believe this means this means that sem_id is getting set to a true value. You should group the operation in the if statement.
if((sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT)) == -1){
Related
I tried to attach a char vector to a shared memory: shmget() is ok but shmat() returns an error.
This is my code:
...
...
#define TXTSZ 512
---main---
char *address;
int shm_id;
...
if(shm_id = shmget(IPC_PRIVATE, TXTSZ*sizeof(char), IPC_CREAT | 0666) == -1){
perror("Error shmget");
}
...
if((address = (char *)shmat(shm_id, NULL, 0)) == (char *) -1){
perror("Error shmat");
}
...
...
Classic mistake (usually made with open()). You're setting shm_id to the result of the comparison, not the ID returned by shmget.
You need parentheses around the assignment.
if((shm_id = shmget(IPC_PRIVATE, TXTSZ*sizeof(char), IPC_CREAT | 0666)) == -1){
I'm writing a simple shell in C and encountered a minor problem.
I have the following function:
int execStdErr(char** parsedArguments, int numberOfArgs) {
fflush(stderr);
int fd;
int parsedCommandLength = 0;
char** parsedCommand = parseLine(parsedArguments[0], &parsedCommandLength);
parsedArguments[numberOfArgs - 1] = deleteSpaces(parsedArguments[numberOfArgs - 1]);
if (fork() == 0) {
if (fd = open(parsedArguments[numberOfArgs - 1], O_WRONLY | O_CREAT |O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
perror("lsh");
return 1;
}
if (dup2(fd, 2) < 0) {
perror("lsh") ;
return 1;
}
close(fd);
execvp(parsedCommand[0], parsedCommand);
exit(0);
}
close(fd);
wait(NULL);
return 0;
}
parsedArguments are arguments splitted by 2>, then I take the last one as it is name of my file, and I process the previous one by splitting them on spaces (and they are in parsedCommand). For some reason the stderr prints on screen, it creates a file if it didn't exist but it is always empty. I don't know what might be the problem here.
A common error:
if (fd = open(...) < 0)
is equivalent to
if (fd = (open(...) < 0))
which is not what you want. You need:
if ( (fd = open(...)) < 0)
When open succeeds, open(...) < 0 evaluates to false and fd = open(...) < 0 assigns 0 to fd. The value returned by open, however, is lost.
The command semctl always returns -1 (returns "Fail2" in console). What am I doing wrong?
union semun{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
} forsem;
forsem.val = 0;
int sem;
if((sem= semget(key, 1, 0666 | IPC_CREAT) == -1)) {
fprintf(stderr, "Fail1");
}
if (semctl(sem, 0, SETVAL, forsem) == -1) {
fprintf(stderr, "Fail2");
}
Errno writes Invalid argument
You make a simple mistake: in
if((sem= semget(key, 1, 0666 | IPC_CREAT) == -1)) {
fprintf(stderr, "Fail1");
}
You should write
if((sem= semget(key, 1, 0666 | IPC_CREAT)) == -1) {
fprintf(stderr, "Fail1");
}
Notice the brackets?
By the way, the error is EIDRM because sem is 0 in your code, not Invalid Argument.
Thanks for your answer! It does not matter. The only thing it's not giving error is for semctl with the second argument 0 (number of the semaphore in the semaphore set), if I put 1 or 30 there, it returns -1.
I want create a semaphore counter, with this code:
union semun arg_assistant;
int max_ass = atoi(argv[1]);
printf("Num massimo di assistant %d\n", max_ass);
fflush(stdout);
if ((sem_a = semget(IPC_PRIVATE, 1, 0600)) == -1) {
perror("semget");
exit(EXIT_FAILURE);
}
arg_assistant.val = max_ass;
if (semctl(sem_a, 0, SETALL, arg_assistant) == -1) {
perror("semctl");
exit(EXIT_FAILURE);
}
When I executed my program, I have no errors but it hangs and it don't create this sem. Any suggestion about what could be the problem? Have I make some mistake with falgs?
Thaks
From documentation
SETALL
Set semval for all semaphores of the set using arg.array,
For SETALL you need array of values
unsigned short int sem_array[1] ;
sem_array[0] = max_ass;
arg_assistant.array = sem_array;
if (semctl(sem_a, 0, SETALL, arg_assistant) == -1) {
perror("semctl");
exit(EXIT_FAILURE);
}
sem_array[1] becaues you create only one semaphore.
i open a file and want to write something in it. The problem is that the fd2 for some reason is 0. Instead of writting in the file, it writes on terminal. I dont close(0) anywhere in my code. Why do i get fd = 0 and not for example 3. The reason that writes on terminal is that the value of fd is zero? I know that fd = 0 is for standard input,
Any Ideas? Thank you.
if ((fd2 = open(logFile, O_RDWR |O_APPEND | O_CREAT , 0666) == -1))
DieWithError("open() failed");
printf("FD2 = %d",fd2); //returns me zero
bzero(tempStr, sizeof(tempStr));
bzero(hostname, sizeof(hostname));
gethostname(hostname, sizeof(hostname));
sprintf(tempStr, "\n%sStarting FTP Server on host %s in port %d\n", ctime(¤time), hostname, port);
if (write(fd2, tempStr, strlen(tempStr)) == -1)
DieWithError("write(): failed");
Your conditional is off. Mind the parentheses. It should be:
if ((fd2 = open(logFile, O_RDWR |O_APPEND | O_CREAT , 0666)) == -1)
// ^^^ ^^^
Sometimes it might be best not to outsmart yourself:
int fd = open(...);
if (fd == -1) { DieWithError(); }
This is wrong.
if ((fd2 = open(logFile, O_RDWR |O_APPEND | O_CREAT , 0666) == -1))
You want this.
if ((fd2 = open(logFile, O_RDWR |O_APPEND | O_CREAT , 0666)) == -1)
It's hard to see because the line is so long, but the parentheses are in the wrong place. In short,
if (( fd2 = open(...) == -1 )) // your code
if (( fd2 = (open(...) == -1) )) // equivalent code
if (( (fd2 = open(...)) == -1) )) // correct code
If the line is so long, best to keep it out of the if...
#include <err.h>
fd2 = open(...);
if (fd2 < 0)
err(1, "open failed");