Ejemplo n.º 1
0
static int gxdir_next(Dir *dir)
{
	GXDir *gxdir = (GXDir*) dir->fsdata;
	semWait(&gxdir->gxfs->sem);

	if ((gxdir->index++) == gxdir->count)
	{
		semSignal(&gxdir->gxfs->sem);
		return -1;
	};

	gxdir->offCurrent = gxdir->gxino.pos;

	char buffer[257];
	GXReadInode(&gxdir->gxino, buffer, gxdir->szNext);
	buffer[gxdir->szNext] = 0;

	gxfsDirent *ent = (gxfsDirent*) buffer;
	gxdir->szNext = ent->deNextSz;
	if (ent->deNameLen > 127)
	{
		semSignal(&gxdir->gxfs->sem);
		return -1;
	};

	strcpy(dir->dirent.d_name, ent->deName);
	dir->dirent.d_ino = ent->deInode;
	//kprintf_debug("NEXT INODE : %d\n", ent->deInode);
	if (dir->dirent.d_ino == 0) dir->dirent.d_name[0] = 0;

	//kprintf_debug("NEXT: %s (%d)\n", dir->dirent.d_name, dir->dirent.d_ino);

	semSignal(&gxdir->gxfs->sem);
	return 0;
};
Ejemplo n.º 2
0
// **********************************************************************
// timer interrupt service routine
//
static void timer_isr()
{
	time_t currentTime;						// current time

	// assert system mode
	assert("timer_isr Error" && superMode);

	// capture current time
  	time(&currentTime);

  	// one second timer
  	if ((currentTime - oldTime1) >= 1)
  	{
		// signal 1 second
  	   semSignal(tics1sec);
		oldTime1 += 1;
  	}

	// sample fine clock
	myClkTime = clock();
	if ((myClkTime - myOldClkTime) >= ONE_TENTH_SEC)
	{
		myOldClkTime = myOldClkTime + ONE_TENTH_SEC;   // update old
		semSignal(tics10thsec);
	}

	// ?? add other timer sampling/signaling code here for project 2

	return;
} // end timer_isr
Ejemplo n.º 3
0
void termPutChar(char c)
{
	semWait(&semLineBuffer);
	if ((c == '\b') && (termState.c_lflag & ICANON))
	{
		if (lineBufferSize != 0)
		{
			if (inputWrite == 0) inputWrite = INPUT_BUFFER_SIZE;
			else inputWrite--;
			lineBufferSize--;
			if (termState.c_lflag & ECHO) kprintf("\b");
		};

		semSignal(&semLineBuffer);
		return;
	}
	else if (c < 0x80)
	{
		inputBuffer[inputWrite++] = c;
		if (inputWrite == INPUT_BUFFER_SIZE) inputWrite = 0;
		lineBufferSize++;
	};

	__sync_synchronize();
	if (((c & 0x80) == 0) && (termState.c_lflag & ECHO))
	{
		kprintf("%c", c);
	}
	else if ((c == '\n') && (termState.c_lflag & ECHONL))
	{
		kprintf("\n");
	};

	if ((termState.c_lflag & ICANON) == 0)
	{
		semSignal2(&semCount, lineBufferSize);
		lineBufferSize = 0;
	}
	else if (c == '\n')
	{
		semSignal2(&semCount, lineBufferSize);
		lineBufferSize = 0;
	};

	if ((unsigned char)c == CC_VINTR)
	{
		if (termState.c_lflag & ECHO) kprintf("^C");
		if (termState.c_lflag & ICANON)
		{
			inputWrite = inputRead;
			while (semWaitGen(&semCount, 1024, SEM_W_NONBLOCK, 0) > 0);
			lineBufferSize = 0;
		};
		if (termGroup != 0) signalPid(-termGroup, SIGINT);
	};

	semSignal(&semLineBuffer);
};
Ejemplo n.º 4
0
// **********************************************************************
// keyboard interrupt service routine
//
static void keyboard_isr()
{
	// assert system mode
	assert("keyboard_isr Error" && superMode);

	semSignal(charReady);					// SIGNAL(charReady) (No Swap)
	if (charFlag == 0)
	{
		switch (inChar)
		{
			case '\r':
			case '\n':
			{
				inBufIndx = 0;				// EOL, signal line ready
				semSignal(inBufferReady);	// SIGNAL(inBufferReady)
				break;
			}

			case 0x18:						// ^x
			{
				inBufIndx = 0;
				inBuffer[0] = 0;
				sigSignal(0, mySIGINT);		// interrupt task 0
				semSignal(inBufferReady);	// SEM_SIGNAL(inBufferReady)
				break;
			}

			case 0x17: 						// ^w
			{
				sigSignal(-1, mySIGTSTP);
				break;
			}

			case 0x12:						// ^r
			{
				sigSignal (-1, mySIGCONT);

				break;
			}


			default:
			{
				inBuffer[inBufIndx++] = inChar;
				inBuffer[inBufIndx] = 0;
				printf("%c", inChar);		// echo character
			}
		}
	}
	else
	{
		// single character mode
		inBufIndx = 0;
		inBuffer[inBufIndx] = 0;
	}
	return;
} // end keyboard_isr
Ejemplo n.º 5
0
static ssize_t pipe_read(File *fp, void *buffer, size_t size)
{
	if (size == 0) return 0;

	Pipe *pipe = (Pipe*) fp->fsdata;
	semWait(&pipe->sem);

	if (pipe->size == 0)
	{
		if (pipe->writecount == 0)
		{
			semSignal(&pipe->sem);
			return 0;
		}
		else if (fp->oflag & O_NONBLOCK)
		{
			semSignal(&pipe->sem);
			getCurrentThread()->therrno = EAGAIN;
			return -1;
		}
		else
		{
			semSignal(&pipe->sem);
			while (1)
			{
				if (getCurrentThread()->sigcnt > 0)
				{
					getCurrentThread()->therrno = EINTR;
					return -1;
				};

				semWait(&pipe->sem);
				if (pipe->size > 0) break;
				semSignal(&pipe->sem);
			};
		};
	};

	if (size > pipe->size) size = pipe->size;

	ssize_t outSize = 0;
	uint8_t *out = (uint8_t*) buffer;
	while (size--)
	{
		*out++ = pipe->buffer[pipe->offRead];
		pipe->offRead = (pipe->offRead + 1) % 1024;
		outSize++;
		pipe->size--;
	};

	semSignal(&pipe->sem);
	return outSize;
};
Ejemplo n.º 6
0
static ssize_t pipe_write(File *fp, const void *buffer, size_t size)
{
	if (size == 0) return 0;

	Pipe *pipe = (Pipe*) fp->fsdata;
	semWait(&pipe->sem);

	if (pipe->readcount == 0)
	{
		semSignal(&pipe->sem);
		return 0;
	};

	if ((pipe->size+size) > 1024)
	{
		if (fp->oflag & O_NONBLOCK)
		{
			semSignal(&pipe->sem);
			getCurrentThread()->therrno = EAGAIN;
			return -1;
		}
		else
		{
			semSignal(&pipe->sem);
			while (1)
			{
				if (getCurrentThread()->sigcnt > 0)
				{
					getCurrentThread()->therrno = EINTR;
					return -1;
				};
				semWait(&pipe->sem);
				if ((pipe->size+size) <= 1024) break;
				semSignal(&pipe->sem);
			};
		};
	};

	ssize_t inSize = 0;
	const uint8_t *in = (const uint8_t*) buffer;
	while (size--)
	{
		pipe->buffer[pipe->offWrite] = *in++;;
		pipe->offWrite = (pipe->offWrite + 1) % 1024;
		inSize++;
		pipe->size++;
	};

	semSignal(&pipe->sem);
	return inSize;
};
Ejemplo n.º 7
0
static int gxdir_chown(Dir *dir, uid_t uid, gid_t gid)
{
	time_t now = time();

	GXDir *gxdir = (GXDir*) dir->fsdata;
	semWait(&gxdir->gxfs->sem);

	//kprintf_debug("gxfs: chmod on inode %d\n", dir->dirent.d_ino);
	GXInode gxino;
	GXOpenInode(gxdir->gxfs, &gxino, dir->dirent.d_ino);

	gxfsInode inode;
	GXReadInodeHeader(&gxino, &inode);

	inode.inoOwner = (uint16_t) uid;
	inode.inoGroup = (uint16_t) gid;
	inode.inoCTime = now;
	GXWriteInodeHeader(&gxino, &inode);

	dir->stat.st_mode = (mode_t) inode.inoMode;
	dir->stat.st_ctime = inode.inoCTime;

	if (gxdir->gxfs->fp->fsync != NULL) gxdir->gxfs->fp->fsync(gxdir->gxfs->fp);

	semSignal(&gxdir->gxfs->sem);
	return 0;
};
Ejemplo n.º 8
0
static int gxdir_chmod(Dir *dir, mode_t mode)
{
	time_t now = time();

	GXDir *gxdir = (GXDir*) dir->fsdata;
	semWait(&gxdir->gxfs->sem);

	//kprintf_debug("gxfs: chmod on inode %d\n", dir->dirent.d_ino);
	GXInode gxino;
	GXOpenInode(gxdir->gxfs, &gxino, dir->dirent.d_ino);

	gxfsInode inode;
	GXReadInodeHeader(&gxino, &inode);

	inode.inoMode = (inode.inoMode & 0xF000) | (mode & 0x0FFF);
	inode.inoCTime = now;
	GXWriteInodeHeader(&gxino, &inode);

	dir->stat.st_mode = (mode_t) inode.inoMode;
	dir->stat.st_ctime = inode.inoCTime;

	if (gxdir->gxfs->fp->fsync != NULL) gxdir->gxfs->fp->fsync(gxdir->gxfs->fp);

	semSignal(&gxdir->gxfs->sem);
	return 0;
};
Ejemplo n.º 9
0
static File *openPipe(Pipe *pipe, int mode)
{
	semWait(&pipe->sem);
	File *fp = (File*) kmalloc(sizeof(File));
	memset(fp, 0, sizeof(File));

	if (mode == O_RDONLY)
	{
		pipe->readcount++;
	}
	else
	{
		pipe->writecount++;
	};

	fp->fsdata = pipe;
	fp->oflag = mode;
	fp->read = pipe_read;
	fp->write = pipe_write;
	fp->close = pipe_close;
	fp->dup = pipe_dup;
	fp->fstat = pipe_fstat;

	semSignal(&pipe->sem);
	return fp;
};
Ejemplo n.º 10
0
// **********************************************************************
// system kill task
//
int sysKillTask(int taskId)
{
	Semaphore* sem = semaphoreList;
	Semaphore** semLink = &semaphoreList;

	// assert that you are not pulling the rug out from under yourself!
	assert("sysKillTask Error" && tcb[taskId].name && superMode);
	printf("\nKill Task %s", tcb[taskId].name);

	// signal task terminated
	semSignal(taskSems[taskId]);

	// look for any semaphores created by this task
	while(sem = *semLink)
	{
		if(sem->taskNum == taskId)
		{
			// semaphore found, delete from list, release memory
			deleteSemaphore(semLink);
		}
		else
		{
			// move to next semaphore
			semLink = (Semaphore**)&sem->semLink;
		}
	}

	// ?? delete task from system queues

	free (tcb[taskId].argv);
	tcb[taskId].name = 0;			// release tcb slot
	return 0;
} // end sysKillTask
Ejemplo n.º 11
0
void brelse(buffer *buff) {

    debug(BUFFER_DL, "brelse(buff:%p) {\n", buff);

    // wait for semaphore to start
    while (!semWait(semid)) {
        // skip signal interruptions
        if (errno != EINTR) {
            perror("brelse, semWait");
            debug(2 + BUFFER_DL, "ERROR en brelse, semWait.");
            exit(1);
        }
    }

    // enqueue buffer at end of free list
    setfree(buff);

    // unlock buffer
    BS_CLR(buff->status, BS_LOCKED);

    // wakeup all procs waiting for any buffer to become free
    brelease(NULL);

    // wakeup all procs waiting for this buffer to become free
    brelease(buff);

    // free the semaphore
    if (!semSignal(semid)) {
        perror("brelse, semSignal");
        debug(2 + BUFFER_DL, "ERROR en brelse, semSignal.");
        exit(1);
    }

    debug(BUFFER_DL, "}\n");
}
Ejemplo n.º 12
0
static XObj
bidCreateSessn(
    XObj	self,
    XObj	hlpRcv,
    XObj	hlpType,
    ActiveKey	*key,
    Path	path)
{
    PState	*ps = (PState *)self->state;
    XObj	s;

    /* 
     * We might block (but only briefly), so we'll only allow one
     * thread to create a session at a time to avoid map binding
     * conflicts. 
     */
    semWait(&ps->sessnCreationSem);
    /* 
     * Check again now that we have the lock
     */
    if ( mapResolve(ps->activeMap, key, &s) == XK_FAILURE ) {
	s = bidNewSessn(self, hlpRcv, hlpType, key, path);
    }
    semSignal(&ps->sessnCreationSem);
    return s;
}
Ejemplo n.º 13
0
void tcpsock_shutdown(Socket *sock, int shutflags)
{
	TCPSocket *tcpsock = (TCPSocket*) sock;
	
	semWait(&tcpsock->lock);
	
	if (shutflags & SHUT_RD)
	{
		if ((tcpsock->shutflags & SHUT_RD) == 0)
		{
			tcpsock->shutflags |= SHUT_RD;
			// do not terminate the receive semaphore. this should only happen
			// if we actually receive a FIN.
		};
	};
	
	if (shutflags & SHUT_WR)
	{
		if ((tcpsock->shutflags & SHUT_WR) == 0)
		{
			tcpsock->shutflags |= SHUT_WR;
			semTerminate(&tcpsock->semSendPut);
		};
	};
	
	semSignal(&tcpsock->lock);
};
Ejemplo n.º 14
0
static void isofile_close(File *fp)
{
	ISOFile *file = (ISOFile*) fp->fsdata;
	semWait(&file->isofs->sem);
	file->isofs->numOpenInodes--;
	semSignal(&file->isofs->sem);
	kfree(fp->fsdata);
};
Ejemplo n.º 15
0
static void gxdir_close(Dir *dir)
{
	GXDir *gxdir = (GXDir*) dir->fsdata;
	semWait(&gxdir->gxfs->sem);

	gxdir->gxfs->numOpenInodes--;
	semSignal(&gxdir->gxfs->sem);
	kfree(dir->fsdata);
};
Ejemplo n.º 16
0
int main(int argc, char const *argv[])
{
	pid = getpid();
	printf("Pid of this Client is: %d\n",pid);

	//////////semGet//////////////////
	semid = semGet(SEMNUM);
	printf("semid: %d\n",semid);

	//////opeing famous fifo to place request///////	
	fd = open(famfifo,O_WRONLY);
	assert(fd != -1);

	/////place request////////////
	struct Request req;
	req.pid = pid;

	semWait(semid,0);			//acquire mutex
	write(fd,&req,REQSIZE); 	//place request
	semSignal(semid,1); 		//notify server that request is available
	semWait(semid,1); 			//wait until fifo gets created by server
	genearteFifo(pid);     //opnes 2 fifo names pid.in and pid.out already created by server
	semSignal(semid,0);			//release mutex
	///////////////////////////////////////////////////////

	///////////////Server to poll msg from other clients/////////////
	assert(pthread_create(&tid,NULL,pollServer,NULL) == 0);

	/////////////Take Msg from STDIN//////////////////
	char buff[BUFFSIZE];
	while(1)
	{
		int pos=0;
		char ch;
		struct Msg msg;
		printf("write===: ");
		while((ch = getchar()) != '\n')
			msg.data[pos++] = ch;
		msg.pid = pid;
		msg.data[pos] = '\0';
		write(ofd,&msg,MSGSIZE);
	}
	return 0;
}
Ejemplo n.º 17
0
int main(int argc, char *argv[]) {
	struct busTicket myTicket;

	pname = argc > 0 ? argv[1] : "CUSTOMER";
	pid = getpid();
	printInfo("Initializing Customer");
	/* initialize shared resource variables */
	locateResources(&semid, &shmid, &shm);

	/* get in line */
	semWait(semid, SEM_LINE);
	printInfo("My turn in line");

	/* wait for mutex */
	semWait(semid, SEM_MUTEX);
	
	/* tell agent my name */
	snprintf(shm->name, sizeof(shm->name), "%s", pname);
	semSignal(semid, SEM_AGENT);

	/* wait for my ticket to be assigned */
	semWait(semid, SEM_TICKET);
	
	/* get the ticket */
	myTicket = shm->ticket;
	sprintf(buf, "My Ticket: [Name:%s Seat:%d Depart:%ld]",
		myTicket.name, myTicket.seat, myTicket.depart);
	printInfo(buf);

	/* release mutex */
	if(myTicket.depart == shm->nbDepart) {
		printInfo("Waiting for next bus");
		shm->nbWait++;
		semSignal(semid, SEM_MUTEX);
		/* wait for next bus */
		semWait(semid, SEM_NBUS);
	} else {
		semSignal(semid, SEM_MUTEX);
	}

	/* board the bus */
	printInfo("Boarding the bus");
	return EXIT_SUCCESS;
}
Ejemplo n.º 18
0
static int isofile_dup(File *me, File *fp, size_t szfile)
{
	ISOFile *org = (ISOFile*) me->fsdata;
	semWait(&org->isofs->sem);
	org->isofs->numOpenInodes++;
	ISOFile *file = (ISOFile*) kmalloc(sizeof(ISOFile));
	memcpy(file, me->fsdata, sizeof(ISOFile));
	memcpy(fp, me, szfile);
	fp->fsdata = file;
	semSignal(&org->isofs->sem);
	return 0;
};
Ejemplo n.º 19
0
static int
deliverSWP(SwpState state, Msg *frame)
{
    SwpHdr hdr;
    char *hbuf;
    
    hbuf = msgStripHdr(frame, HLEN);
    load_swp_hdr(&hdr, hbuf);
    if (hdr->Flags & FLAG_ACK_VALID)
    {
        if (swpInWindow(hdr.AckNum, state->LAR + 1, state->LFS))
        {
            do {
                struct sendQ_slot *slot;
                slot = &state->sendQ[++state->LAR % SWS];
                evCancel(slot->timeout);
                msgDestroy(&slot->msg);
                semSignal(&state->sendWindowNotFull);
            } while (state->LAR != hdr.Acknum);
        }
    }

    if (hdr.Flags & FLAG_HAS_DATA)
    {
        struct recvQ_slot, *slot;
        //received data packet---do RECEIVER side
        slot = &state->recvQ[hdr.SeqNum % RWS];
        if (!swpInWindow(hdr.SeqNum, state->NFE, 
                    state->NFE + RWS - 1)) {
            //drop the message
            return SUCCESS;
        }
        msgSaveCopy(&slot->msg, frame);
        slot->received = TRUE;
        if (hdr.SeqNum == state->NFE) {
            Msg m;
            
            while (slot->received) {
                deliver(HLP, &slot->msg);
                msgDestroy(&slot->msg);
                slot->received = FALSE;
                slot = &state->recvQ[++state->NFE % RWS];
            }
            //send ACK
            prepare_ack(&m, state->NFE - 1);
            send(LINK, &m);
            msgDestroy(&m);
        }
    }
    return SUCCESS;

}
Ejemplo n.º 20
0
void
dgram_abort(
	    Event	ev,
	    void 	*arg)
{
    XObj        self = (XObj)arg;
    PState 	*ps = (PState *)self->state;

    dgram_abort_count++;
    ps->partialcount = 0;
    semSignal(&ps->sema);
    ps->flushing = TRUE;
}
Ejemplo n.º 21
0
int pipeWrite(struct Pipe *pipe, const void *buffer, size_t size) {
	const char *cbuf = (const char *)buffer;
	while (size) {
		semWait(&pipe->bytesFree);
		pipe->buf[pipe->wIndex] = *cbuf;
		semSignal(&pipe->bytesUsed);

		pipe->wIndex = (pipe->wIndex + 1) % PIPE_BUF;
		cbuf++;
		size--;
	}
	return 0;
}
Ejemplo n.º 22
0
ssize_t pipeRead(struct Pipe *pipe, void *buffer, size_t size) {
	char *cbuf = (char *)buffer;
	ssize_t ret = 0;
	while (ret != (ssize_t)size) {
		semWait(&pipe->bytesUsed);
		*cbuf = pipe->buf[pipe->rIndex];
		semSignal(&pipe->bytesFree);

		pipe->rIndex = (pipe->rIndex + 1) % PIPE_BUF;
		ret++;
		cbuf++;
	}
	return ret;
}
Ejemplo n.º 23
0
static int tcpsock_listen(Socket *sock, int backlog)
{
	TCPSocket *tcpsock = (TCPSocket*) sock;
	semWait(&tcpsock->lock);
	if (tcpsock->state != TCP_CLOSED)
	{
		semSignal(&tcpsock->lock);
		ERRNO = EALREADY;
		return -1;
	};

	if (sock->domain == AF_INET)
	{
		struct sockaddr_in *inname = (struct sockaddr_in*) &tcpsock->sockname;
		if (inname->sin_family == AF_UNSPEC)
		{
			inname->sin_family = AF_INET;
			memset(&inname->sin_addr, 0, 4);
			inname->sin_port = AllocPort();
		};
	}
	else
	{
		struct sockaddr_in6 *inname = (struct sockaddr_in6*) &tcpsock->sockname;
		if (inname->sin6_family == AF_UNSPEC)
		{
			inname->sin6_family = AF_INET6;
			memset(&inname->sin6_addr, 0, 16);
			inname->sin6_port = AllocPort();
		};
	};
	
	tcpsock->state = TCP_LISTENING;
	semSignal(&tcpsock->lock);
	return 0;
};
Ejemplo n.º 24
0
static ssize_t gxdir_readlink(Dir *dir, char *buffer)
{
	GXDir *gxdir = (GXDir*) dir->fsdata;
	semWait(&gxdir->gxfs->sem);

	GXInode gxInode;
	GXOpenInode(gxdir->gxfs, &gxInode, dir->dirent.d_ino);

	gxfsInode inode;
	GXReadInodeHeader(&gxInode, &inode);
	strcpy(buffer, (const char*) inode.inoFrags);

	semSignal(&gxdir->gxfs->sem);

	return strlen(buffer);
};
Ejemplo n.º 25
0
int recvFileList(int sfd, struct actionParameters *ap,
		struct serverActionParameters *sap){
	int res;
	struct flEntry file;
	struct array *fl2; 

	//memmove(&file.ip,&ap->comip, sizeof(struct sockaddr));
	file.ip = ap->comip;
	while (( res = getTokenFromStream( sfd, &ap->combuf, &ap->comline, "\n", 
			"\r\n",NULL ))){
		if (res ==  -1) return -1;

		strcpy(file.filename, (char *)ap->comline.buf);
		flushBuf(&ap->comline);

		if (getTokenFromStream( sfd, &ap->combuf, &ap->comline, 
          "\n", "\r\n",NULL ) == -1)
			return -1;
		long size = my_strtol((char *)ap->comline.buf);

		if ( errno || size < 0 ) {
      file.size = size;
			logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, 
          "problem converting file size for %s - %s\n", file.filename, 
          ap->comline.buf);
			continue;
		}
		logmsg(ap->semid, ap->logfd, LOGLEVEL_VERBOSE, "recvFileList: %s - %lu\n",
        file.filename, file.size);

		if ( (res = semWait(ap->semid, SEM_FILELIST)))
			logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, 
          "(recvfile) can't get semaphore, %d", res);
		fl2 = addArrayItem(sap->filelist, &file);
		if ( (res = semSignal(ap->semid, SEM_FILELIST)))
			logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, 
          "(recvfile) can't release semaphore, %d", res);
		if (fl2) sap->filelist = fl2;
		else {
			logmsg(ap->semid, ap->logfd, LOGLEVEL_FATAL, 
          "leaving the functions because realloc failed %d", fl2);
			return -1;
		}
	}
	
	return 1;
}
Ejemplo n.º 26
0
int main(int argc, char const *argv[])
{
	setbuf(stdout, NULL);
	printf("%s created============\n",servername);
	/////////SemGet and Init///////////
	semid = semGet(SEMNUM);
	printf("semid: %d\n",semid);
	unsigned short arr[] = {1,0};
	semInit(semid,arr,SEMNUM);

	////////////////create famfifo///////////////
	unlink(famfifo);
	int ret = mkfifo(famfifo,0666);
	assert(ret != -1);
	ffd = open(famfifo,O_RDWR);
	assert(ffd != -1);
	////////////////////////////////////////////

	//////////Create another thread to poll clients/////////
	assert(pthread_mutex_init(&mut,NULL) == 0);
	assert(pthread_create(&tid,NULL,pollFifo,NULL) == 0);

	struct Request req;
	int i;

	while(1)
	{
		semWait(semid,1);
		read(ffd,&req,REQSIZE);
		printf("Request received from: %d\n",req.pid);
		genearteFifo(req.pid);
		cfd[numclients][INSTREAM] = ifd;
		cfd[numclients][OUTSTREAM]  = ofd;
		pfd[numclients].fd = ifd;
		pfd[numclients].events = POLLIN;
		pfd[numclients].revents = 0;
		////////Use MUTEX to CHANGE tHIS exclusively///////////
		pthread_mutex_lock(&mut);
		numclients++;
		printf("numclients %d\n",numclients);
		pthread_mutex_unlock(&mut);
		//////////////////////////////////////
		semSignal(semid,1);
	}

	return 0;
}
Ejemplo n.º 27
0
static int gxdir_unlink(Dir *dir)
{
	GXDir *gxdir = (GXDir*) dir->fsdata;
	GXFileSystem *gxfs = gxdir->gxfs;
	if (dir->dirent.d_ino < gxfs->cis.cisFirstDataIno)
	{
		return -1;
	};
	semWait(&gxdir->gxfs->sem);

	gxfsInode parentInode;
	GXReadInodeHeader(&gxdir->gxino, &parentInode);
	parentInode.inoCTime = time();
	parentInode.inoMTime = time();
	parentInode.inoLinks--;					// file count decreases
	GXWriteInodeHeader(&gxdir->gxino, &parentInode);

	// rermove the current entry by setting the inode to 0.
	uint64_t inodeZero = 0;
	gxdir->gxino.pos = gxdir->offCurrent;
	GXWriteInode(&gxdir->gxino, &inodeZero, 8);

#if 0
	if (gxdir->gxino.pos != parentInode.inoSize)
	{
		off_t thisPos = gxdir->gxino.pos - sizeof(struct dirent);
		gxdir->gxino.pos = thisPos;
		struct dirent ent;
		memset(&ent, 0, sizeof(struct dirent));
		GXWriteInode(&gxdir->gxino, &ent, sizeof(struct dirent));
	}
	else
	{
		GXShrinkInode(&gxdir->gxino, sizeof(struct dirent), &parentInode);
		GXWriteInodeHeader(&gxdir->gxino, &parentInode);
	};
#endif

	// TODO: file opens should add to inoLinks!
	GXInode gxino;
	GXOpenInode(gxfs, &gxino, dir->dirent.d_ino);
	GXUnlinkInode(&gxino);

	semSignal(&gxdir->gxfs->sem);
	return 0;
};
Ejemplo n.º 28
0
// **********************************************************************
// system kill task
//
int sysKillTask(int taskId)
{
	Semaphore* sem = semaphoreList;
	Semaphore** semLink = &semaphoreList;

	// assert that you are not pulling the rug out from under yourself!
	assert("sysKillTask Error" && tcb[taskId].name && superMode);
	printf("\nKill Task %s \n", tcb[taskId].name);

	// signal task terminated
	semSignal(taskSems[taskId]);

	// look for any semaphores created by this task
	while(sem = *semLink)
	{
		if(sem->taskNum == taskId)
		{
			// semaphore found, delete from list, release memory
			deleteSemaphore(semLink);
		}
		else
		{
			// move to next semaphore
			semLink = (Semaphore**)&sem->semLink;
		}
	}

	// ?? delete task from system queues
	dequeue(readyQueue, taskId); // SCOTT; others? flag

	// SCOTT free mallocs
	int i, numArgs;
	numArgs = tcb[taskId].argc;
	for (i = 0; i < tcb[taskId].argc; i++) {
		free(tcb[taskId].argv[i]); // FLAG FIX
	}
	free(tcb[taskId].argv);

	//println("argv memory free'd!");
	// end SCOTT free mallocs

	tcb[taskId].name = 0;			// release tcb slot
	return 0;
} // end sysKillTask
Ejemplo n.º 29
0
int main(int argc, char const *argv[])
{
	semkey = ftok("/tmp",64);
	assert((semid = semget(semkey,1,IPC_CREAT|0666)) != -1);

	char pfifo[BUFFSIZE];
	int tfd;
	sprintf(pfifo,"%d.out",getpid());
	assert((tfd = open(pfifo,O_RDWR))!=-1);
	struct GroupComm  gc;
	assert(read(tfd,&gc,GCSIZE) >= 0);
	sprintf(pfifo,"GSERVER: port no. received: %d\n",gc.port);
	assert(write(1,pfifo,strlen(pfifo)) >= 0);

	int sfd,clsize;
	struct sockaddr_in servaddr,claddr;
	assert((sfd = socket(AF_INET,SOCK_STREAM,0)) != -1);
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(gc.port);
	servaddr.sin_addr.s_addr = INADDR_ANY;
	assert(bind(sfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) != -1);
	assert(listen(sfd,QLEN) != -1);

	semSignal(semid,0);

	pthread_t tid;
	pthread_mutex_init(&mut,NULL);
	assert(pthread_create(&tid,NULL,serve,NULL) != -1);
	
	while(1){
		printf("Waiting for clients..........\n");
		int nsfd = accept(sfd,(struct sockaddr*)&claddr,&clsize);
		sprintf(pfifo,"GSERVER: Client Received %d.\n",numclient+1);
		assert(write(1,pfifo,strlen(pfifo)) >= 0);
		pthread_mutex_lock(&mut);
		cfd[numclient].fd = nsfd;
		cfd[numclient].events = POLLIN;
		cfd[numclient].revents = 0;
		numclient++;
		pthread_mutex_unlock(&mut);
	}	
	return 0;
}
Ejemplo n.º 30
0
void registerFSDriver(FSDriver *drv)
{
	semWait(&semFS);
	if (firstDriver == NULL)
	{
		firstDriver = drv;
		drv->prev = NULL;
		drv->next = NULL;
	}
	else
	{
		FSDriver *last = firstDriver;
		while (last->next != NULL) last = last->next;
		last->next = drv;
		drv->prev = last;
		drv->next = NULL;
	};
	semSignal(&semFS);
};