// NewProc returns a process for the given pid.
426886
func (fs FS) NewProc(pid int) (Proc, error) {
	if _, err := os.Stat(fs.Path(strconv.Itoa(pid))); err != nil {
		return Proc{}, err
	}
	return Proc{PID: pid, fs: fs}, nil
}
1304243
func RunProc(cmd string, shellMethod string, dir string, log termlog.Stream) error {
	log.Header()
	ex, err := shell.NewExecutor(shellMethod, cmd, dir)
	if err != nil {
		return err
	}
	start := time.Now()
	err, estate := ex.Run(log, true)
	if err != nil {
		return err
	} else if estate.Error != nil {
		log.Shout("%s", estate.Error)
		return ProcError{estate.Error.Error(), estate.ErrOutput}
	}
	log.Notice(">> done (%s)", time.Since(start))
	return nil
}
944306
func Kill(pid int64) error {
	for _, proc := range Processes {
		if proc.PID == pid {
			if proc.Cmd != nil && proc.Cmd.Process != nil &&
				proc.Cmd.ProcessState != nil && !proc.Cmd.ProcessState.Exited() {
				if err := proc.Cmd.Process.Kill(); err != nil {
					return fmt.Errorf("fail to kill process [pid: %d, desc: %s]: %v", proc.PID, proc.Description, err)
				}
			}
			Remove(pid)
			return nil
		}
	}
	return nil
}
1217244
func (as ArraySpec) proc(f func(Input) Input) Input {
	var nas ArraySpec
	for i := range []int{0, 1, 2} {
		for j := range nas[i] {
			nas[i][j] = f(as[i][j])
		}
	}
	return nas
}
371990
func NewProcess(committer execution.BatchCommitter, blockchain *bcm.Blockchain, txDecoder txs.Decoder,
	commitInterval time.Duration, panicFunc func(error)) *Process {

	p := &Process{
		committer:  committer,
		blockchain: blockchain,
		done:       make(chan struct{}),
		txDecoder:  txDecoder,
		panic:      panicFunc,
	}

	if commitInterval != 0 {
		p.ticker = time.NewTicker(commitInterval)
		go p.triggerCommits()
	}

	return p
}
774379
func NewProcMetricsWith(eng *stats.Engine, pid int) *ProcMetrics {
	p := &ProcMetrics{engine: eng, pid: pid}

	p.cpu.user.typ = "user"
	p.cpu.system.typ = "system"

	p.memory.resident.typ = "resident"
	p.memory.shared.typ = "shared"
	p.memory.text.typ = "text"
	p.memory.data.typ = "data"

	p.memory.pagefault.major.typ = "major"
	p.memory.pagefault.minor.typ = "minor"

	p.threads.switches.voluntary.typ = "voluntary"
	p.threads.switches.involuntary.typ = "involuntary"

	return p
}
1318214
func NewProc(fname string) (*Proc, error) {
	f, err := os.Open(fname)
	if err != nil {
		return nil, err
	}
	return &Proc{File: f, Buf: bufio.NewReader(f)}, nil
}
1340057
func NewMockProcessManager(ctrl *gomock.Controller) *MockProcessManager {
	mock := &MockProcessManager{ctrl: ctrl}
	mock.recorder = &MockProcessManagerMockRecorder{mock}
	return mock
}
1184469
func NewProcessManager() *ProcessManager {
	// We use pseudo PIDs that don't conflict with OS PIDs, so they can live in the same table.
	// For the pseudo PIDs, we use a sync.Pool rather than a plain old counter to avoid the unlikely,
	// but possible wrapping should such a counter exceed MaxInt64.
	pid := int64(32768) // TODO: /proc/sys/kernel/pid_max

	return &ProcessManager{
		expire:  time.Minute * 5,
		entries: make(map[int64]*Process),
		pids: sync.Pool{
			New: func() interface{} {
				return atomic.AddInt64(&pid, 1)
			},
		},
	}
}
1097824
func (container *container) CreateProcess(c *ProcessConfig) (Process, error) {
	p, err := container.system.CreateProcess(c)
	if err != nil {
		return nil, convertSystemError(err, container)
	}
	return &process{p: p}, nil
}
228757
func (computeSystem *System) OpenProcess(pid int) (_ *Process, err error) {
	computeSystem.handleLock.RLock()
	defer computeSystem.handleLock.RUnlock()

	// Add PID for the context of this operation
	computeSystem.logctx[logfields.ProcessID] = pid
	defer delete(computeSystem.logctx, logfields.ProcessID)

	operation := "hcsshim::ComputeSystem::OpenProcess"
	computeSystem.logOperationBegin(operation)
	defer func() { computeSystem.logOperationEnd(operation, err) }()

	var (
		processHandle hcsProcess
		resultp       *uint16
	)

	if computeSystem.handle == 0 {
		return nil, makeSystemError(computeSystem, "OpenProcess", "", ErrAlreadyClosed, nil)
	}

	syscallWatcher(computeSystem.logctx, func() {
		err = hcsOpenProcess(computeSystem.handle, uint32(pid), &processHandle, &resultp)
	})
	events := processHcsResult(resultp)
	if err != nil {
		return nil, makeSystemError(computeSystem, "OpenProcess", "", err, events)
	}

	process := newProcess(processHandle, pid, computeSystem)
	if err = process.registerCallback(); err != nil {
		return nil, makeSystemError(computeSystem, "OpenProcess", "", err, nil)
	}
	go process.waitBackground()

	return process, nil
}
443597
func (e *UniversalExecutor) shutdownProcess(_ os.Signal, proc *os.Process) error {
	if err := sendCtrlBreak(proc.Pid); err != nil {
		return fmt.Errorf("executor shutdown error: %v", err)
	}
	e.logger.Debug("sent Ctrl-Break to process", "pid", proc.Pid)

	return nil
}
1150669
func (c *Container) Process(id string) (rproc.Process, error) {
	c.mu.Lock()
	defer c.mu.Unlock()
	if id == "" {
		if c.process == nil {
			return nil, errors.Wrapf(errdefs.ErrFailedPrecondition, "container must be created")
		}
		return c.process, nil
	}
	p, ok := c.processes[id]
	if !ok {
		return nil, errors.Wrapf(errdefs.ErrNotFound, "process does not exist %s", id)
	}
	return p, nil
}
147273
func NewProcessInfo(priority level.Priority, pid int32, message string) Composer {
	p := &ProcessInfo{
		Message: message,
		Pid:     pid,
	}

	if err := p.SetPriority(priority); err != nil {
		p.saveError("priority", err)
		return p
	}

	proc, err := process.NewProcess(pid)
	p.saveError("process", err)
	if err != nil {
		return p
	}

	p.loggable = true
	p.populate(proc)

	return p
}
456693
func GetExecPathFromPID(pid uint32) (string, error) {
	return os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
}
286661
func KillProcess(pid int) {
	p, err := os.FindProcess(pid)
	if err == nil {
		p.Kill()
	}
}
309970
func NewProjectionInProcess(ctx *plan.Context, p *plan.Projection) *Projection {
	s := &Projection{
		TaskBase: NewTaskBase(ctx),
		p:        p,
	}
	s.Handler = s.projectionEvaluator(p.Final)
	return s
}